我正在使用selenium和geckodriver尝试获取一个html页面。下面是我正在使用的代码
from pyvirtualdisplay import Display
from selenium import webdriver
#start the virtual display
display = Display(visible=0, size=(800, 600))
display.start()
# start the browser. Using firefox.
browser = webdriver.Firefox()
browser.get('https://www.google.com')
print browser.title
browser.quit()
display.stop()
当我用python执行它时,我收到以下错误消息。
Traceback (most recent call last):
File "test.py", line 9, in <module>
browser = webdriver.Firefox()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 152, in __init__
keep_alive=True)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 98, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 188, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused
我咨询了geckodriver.log
。
1500842848329 geckodriver INFO geckodriver 0.18.0
1500842848362 geckodriver INFO Listening on 127.0.0.1:45467
1500842849526 geckodriver::marionette INFO Starting browser /usr/bin/firefox with args ["-marionette"]
我不确定下一步该往下看。任何提示?
Update1:似乎当我在VPS上进行电源重置时,脚本开始工作。我进一步调查了它,并记得有四个已经失效的进程从之前的firefox执行中运行,我不得不杀死它。我不太了解Linux中进程处理的内部情况,以便完全理解这一点。 geckodriver会变得混乱吗?
我也有这个问题......即使在运行X服务器和功能齐全的KDE环境的机器上也是如此。事实证明,使用geckodriver和Firefox时有严格的版本要求。我使用最新版本的geckodriver运行旧版本的Firefox。一旦我下载了发行说明中列出的版本(并在我的webdriver声明中指定了firefox二进制文件的路径),它运行正常:
driver = webdriver.Firefox(firefox_binary='/path/to/latest/firefox/binary')