python - 使用 Python 教程进行测试驱动开发 : Error trying to open Firefox with Selenium on MacOSX

标签 python macos selenium firefox

我正在学习教程 here而且我无法使用 selenium 打开 Firefox。我都试过了:

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://localhost:8000')

assert 'Django' in browser.title

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

browser = webdriver.Firefox(firefox_binary=FirefoxBinary(
    firefox_path='/Applications/Firefox.app/Contents/MacOS/firefox-bin'
))

assert 'Django' in browser.title

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

browser = webdriver.Firefox(firefox_binary=FirefoxBinary(
    firefox_path='/Applications/Firefox'
))

assert 'Django' in browser.title

对于每个版本,我都会在控制台中收到相同的错误消息:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 64, in start
    stdout=self.log_file, stderr=self.log_file)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 1551, 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 "functional_tests.py", line 5, in <module>
    firefox_path='/Applications/Firefox.app/Contents/MacOS/firefox-bin'
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 135, in __init__
    self.service.start()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 71, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x10312ed30>>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 163, in __del__
    self.stop()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 135, in stop
    if self.process is None:
AttributeError: 'Service' object has no attribute 'process'

我已用尽所有我能想到的看似相关的搜索,但仍未找到解决方案。根据教程,我指向 (45) 的 Firefox 版本应该与 Selenium 兼容...我找不到我在教程中遗漏的任何可能导致此问题的内容。

最佳答案

终于找到了解决办法。显然,从 Selenium v​​3 开始,不再原生支持 Firefox。所以,(仍然使用 Firefox v45)我找到了一个解决方案 here并将其与我自己的代码集成。 (我将尝试解释,但这是我第一次使用 Selenium,因此为了更好地理解我建议阅读链接的文章。)

从本质上讲,Selenium webdriver 似乎需要一些配置,我确实喜欢这样。

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

# Bring up the capabilities object for editing and call on FIREFOX
caps = DesiredCapabilities.FIREFOX

# Specify the exact binary of Firefox you want it to point to
caps["binary"] = "/Applications/Firefox.app/Contents/MacOS/firefox-bin"

# Specify geckodriver location
geckodriver = '/Users/<username>/Downloads/geckodriver'

# Now we can call the Firefox webdriver, but this time with specified flags for the capabilities and geckodriver exec location
browser = webdriver.Firefox(capabilities=caps, executable_path=geckodriver)
browser.get('http://localhost:8000')

assert 'Django' in browser.title

关于python - 使用 Python 教程进行测试驱动开发 : Error trying to open Firefox with Selenium on MacOSX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40224188/

相关文章:

java - 运行 Selenium 脚本时 Chrome 无法正确关闭

python - UnavailableInvalidChannel : The channel is not accessible or is invalid. 错误代码 : 403, conda-forge 不工作

python - 在 PyYaml 中转储为 utf-8

python - 如何在sklearn的k-means中检查给定向量的聚类细节

python - 如何在 Microsoft Azure 上的 Python WebJob 中打印到控制台?

macos - 使用 AppleScript 在 Safari 中选择文件

java - 如何选择推文按钮 Selenium Webdriver

Java - OS X - Unicode 损坏的字符串

java - 将 Mac Dock 图标上的放置事件传递给 shell 脚本及其子进程

python - 尝试在Python中减去两个日期并在几小时内(大于24小时时间段)得到结果