【问题标题】:Unable to get Selenium Geckodriver to work on Python - Mac OSX High Sierra无法让 Selenium Geckodriver 在 Python 上工作 - Mac OSX High Sierra
【发布时间】:2017-10-30 19:54:10
【问题描述】:

我一直在努力让用于 Firefox 的 Selenium Geckodriver 在我的 Mac 上运行一段时间,但我就是无法让它运行。

如文档所示,我的 /usr/local/bin/ 目录中有 geckodriver 二进制文件。

每当我尝试执行以下操作时:

from selenium import webdriver
browser = webdriver.Firefox()
type(browser)
browser.get('www.google.com')enter code here

我在 Python 控制台中收到以下错误。

Traceback (most recent call last):
  File "/Users/maxmorin/.thonny/BundledPython36/lib/python3.6/site-
packages/selenium/webdriver/common/service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
  File "/Applications/Thonny.app/Contents/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 707, in __init__
restore_signals, start_new_session)
  File "/Applications/Thonny.app/Contents/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1326, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/maxmorin/Google Drive/Support Team Python Code/Max's Projects/Release Readiness/selenium_test.py", line 2, in <module>
browser = webdriver.Firefox()
  File "/Users/maxmorin/.thonny/BundledPython36/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 144, in __init__
self.service.start()
  File "/Users/maxmorin/.thonny/BundledPython36/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 81, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

【问题讨论】:

    标签: python macos selenium selenium-webdriver


    【解决方案1】:

    您必须设置壁虎驱动程序的路径。一种方法如下:

    driver = webdriver.Firefox(profile, executable_path=r'/pathTo/geckodriver')
    driver.get("https://www.google.com")
    

    确保文件是可执行的:

    • chmod +x geckodriver++

    编辑

    如果您不想在代码中指定 executable_path,则必须将其添加到 PATH 环境变量中:

    export PATH=$PATH:/path/to/geckodriver
    

    【讨论】:

    • 谢谢,它成功了。是否有一种简单的方法可以将其配置为 PATH 位置,这样我就不必每次都放入可执行路径?
    猜你喜欢
    • 1970-01-01
    • 2019-07-25
    • 2018-05-02
    • 2019-02-25
    • 2017-01-23
    • 2019-05-15
    • 1970-01-01
    • 2018-04-02
    • 2018-10-07
    相关资源
    最近更新 更多