gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:03:24 24 4
gpt4 key购买 nike

我正在学习教程 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com