我已经在 Ubuntu 16.04.3 LTS 上安装了 Firefox 驱动程序,它安装在
$ which firefox
/usr/bin/firefox
我正在使用以下简单的 Python 脚本来测试 Selenium
#!/usr/bin/env python
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
display = Display(visible=0, size=(1024, 768))
display.start()
#binary = FirefoxBinary('/usr/bin/firefox')
browser = webdriver.Firefox()
browser.get('http://www.ubuntu.com/')
print(browser.page_source)
browser.close()
display.stop()
我收到以下错误:
File "tests.py", line 16, in <module>
browser = webdriver.Firefox()
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 162, in __init__
keep_alive=True)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 1
完整版本信息:
Distributor ID: Ubuntu
Description: Ubuntu 16.04.3 LTS
Release: 16.04
Codename: xenial
这是 100% 操作系统问题,因为我在其他操作系统(如 Windows 和 macOS)上测试过,代码可以正常工作。任何帮助都将不胜感激。
阅读建议的重复线程提供的解决方案之一是进行调查geckodriver.log
。我geckodriver.log
所说的
1524778083599 geckodriver INFO geckodriver 0.20.1
1524778083603 geckodriver INFO Listening on 127.0.0.1:58448
1524778084607 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "-profile" "/tmp/rust_mozprofile.xBi1jOarZ06h"
Failed to connect to Mir: Failed to connect to server socket: No such file or directory
在命令行中我导出了如下显示
$ export DISPLAY=:0
导出后,geckodriver.log 显示
1524778146749 geckodriver INFO geckodriver 0.20.1
1524778146753 geckodriver INFO Listening on 127.0.0.1:55524
1524778147756 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "-profile" "/tmp/rust_mozprofile.nWOzb0ngG3gm"
但现在我在命令行上收到以下错误
Traceback (most recent call last):
File "tests.py", line 16, in <module>
browser = webdriver.Firefox()
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 162, in __init__
keep_alive=True)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Failed to start browser /usr/bin/firefox: other os error