WebDriverException: 消息: Service /usr/bin/google-chrome 意外退出.



我正在尝试在Python脚本中运行Webdriver,当脚本尝试运行google chrome时,它会以状态代码11退出。

这是 python 脚本:

#!/usr/bin/python3
import time
from selenium import webdriver
driver = webdriver.Chrome('/usr/bin/google-chrome')  # Optional argument, if not specified will search path.
driver.get('http://www.google.com/');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!
driver.quit()

以下是完整输出:

[ec2-user@ip-xxx-xx-xx-xxx pythonscrape]$ python3 test-selenium-chrome.py
Traceback (most recent call last):
File "test-selenium-chrome.py", line 5, in <module>
driver = webdriver.Chrome('/usr/bin/google-chrome')  # Optional argument, if not specified will search path.
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
self.service.start()
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 98, in start
self.assert_process_still_running()
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 111, in assert_process_still_running
% (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service /usr/bin/google-chrome unexpectedly exited. Status code was: -11

有谁知道为什么我的脚本在尝试运行谷歌浏览器时报告错误代码 11?

此错误消息...

selenium.common.exceptions.WebDriverException: Message: Service /usr/bin/google-chrome unexpectedly exited. Status code was: -11

。暗示ChromeDriver无法正确启动/生成新的浏览上下文,即Chrome浏览器会话。

看来你快到了。webdriver.Chrome()的默认参数是ChromeDriver二进制文件的绝对路径。但是,根据最佳实践,您必须按如下方式发送密钥

driver = webdriver.Chrome(executable_path='/path/to/chromedriver')  # Optional argument, if not specified will search path

此外,如果您需要传递Chrome二进制文件的绝对路径,则必须通过chrome.options的实例使用binary_location属性,如下所示:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location = '/path/to/chrome'
driver = webdriver.Chrome(options=options, executable_path='/path/to/chromedriver')
driver.get('http://google.com/')

参考

您可以在以下位置找到详细的讨论:

  • Selenium:WebDriver异常:Chrome无法启动:崩溃,因为google-chrome不再运行,因此ChromeDriver假设Chrome已经崩溃

最新更新