我正在尝试这段代码:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://seleniumhq.org/')
它打开firefox但没有得到链接并显示此消息:
Traceback (most recent call last):
File "new.py", line 3, in <module>
browser = webdriver.Firefox()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
keep_alive=True)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 156, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 245, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused
geckodriver log:
1527001787163 geckodriver INFO geckodriver 0.20.1
1527001787169 geckodriver INFO Listening on 127.0.0.1:51383
1527001788204 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "-profile" "/tmp/rust_mozprofile.IKjgvyUQaThG"
1527001790297 Marionette INFO Listening on port 2828
[
答案 0 :(得分:0)
此错误消息......
selenium.common.exceptions.WebDriverException: Message: connection refused
...意味着由于驱动程序无法启动其内部服务器与python客户端通信而引发WebDriverException
基本webdriver异常。
虽然您提到了 selenium最新版本和浏览器最新版本,但具体细节可以帮助我们更轻松地诊断问题。
同样,虽然你提到了消息,但我没有在 geckodriver log 中看到任何错误。
请尝试以下步骤:
将密钥 executable_path
与值一起传递给 GeckoDriver的绝对路径 如下:
from selenium import webdriver
browser = webdriver.Firefox(executable_path='/path/to/geckodriver')
browser.get('http://seleniumhq.org/')
将 Selenium 升级到当前级别Version 3.12.0。
@Test
。driver.quit()
方法中调用tearDown(){}
以关闭&amp; ;正常销毁 WebDriver 和 Web客户端实例。您可以在以下网址找到详细讨论:
答案 1 :(得分:0)
您可以尝试以下代码:
from selenium import webdriver
driver = webdriver.Firefox(executable_path = r'you web driver full path')
driver.get("http://seleniumhq.org/")
答案 2 :(得分:0)
您需要终止所有firefox进程以释放资源。
将此行添加到kill.sh
kill $(ps aux | awk '/firefox/ {print $2}')
运行
sh kill.sh