selenium.common.exceptions.WebDriverException:消息:未知错误:在ubuntu上执行selenium python脚本时,chrome无法启动

时间:2018-04-19 22:50:54

标签: python selenium ubuntu selenium-webdriver selenium-chromedriver

我在ubuntu上运行python3脚本

import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')  # Last I checked this was necessary.
driver = webdriver.Chrome("/home/admin/web/web.com/public_html/scripts/az/chromedriver", chrome_options=options)

以普通用户身份运行,我收到以下错误:

$ python3 getStock.py
Traceback (most recent call last):
File "getStock.py", line 61, in <module>
    driver = webdriver.Chrome("/home/admin/web/web.com/public_html/scripts/az/chromedriver", chrome_options=options)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__
    desired_capabilities=desired_capabilities)
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: unknown error: chrome failed to start
(Driver info: chromedriver=2.38.551591 (bcc4a2cdef0f6b942b2bb8049068f65340fa2a69),platform=Linux 4.2.0-042stab120.16 x86_64)

也尝试使用sudo;我仍然得到以下错误

$ sudo python3 getStock.py
[sudo] password for admin:
Traceback (most recent call last):
File "getStock.py", line 61, in <module>
    driver = webdriver.Chrome("/home/admin/web/web.com/public_html/scripts/az/chromedriver", chrome_options=options)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__
    desired_capabilities=desired_capabilities)
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: unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.38.551591 (bcc4a2cdef0f6b942b2bb8049068f65340fa2a69),platform=Linux 4.2.0-042stab120.16 x86_64)

不确定发生了什么?我试过跟随;

  1. 我尝试过更新硒;
  2. 我尝试过运行python 2.x和python 3.x
  3. 我尝试以普通用户身份运行脚本 和sudo用户
  4. 我尝试将权限更改为偶数chmod 777
  5. 以下是我的版本:

    Distributor ID: Ubuntu
    Description:    Ubuntu 16.04.3 LTS
    Release:        16.04
    Codename:       xenial
    
    Python 3.5.2
    Requirement already satisfied: selenium in /usr/local/lib/python3.5/dist-packages (3.11.0)
    

3 个答案:

答案 0 :(得分:3)

此错误消息......

selenium.common.exceptions.WebDriverException: Message: unknown error: chrome failed to start

...表示您的 WebClient Chrome 无法启动。

解决方案

您需要传递 executable_path 以及,并参考 ChromeDriver的绝对路径通过单个正斜杠即\以及原始的r开关,如下所示:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
#options.add_argument('--disable-gpu')  # applicable to windows os only
driver = webdriver.Chrome(chrome_options=options, executable_path=r'/home/admin/web/web.com/public_html/scripts/az/chromedriver')

附加步骤

  • 通过 IDE 清理您的项目工作区仅使用所需的依赖项重建项目
  • 使用CCleaner工具清除执行 Test Suite 之前和之后的所有操作系统杂务。
  • 如果您的基本 Web客户端版本太旧,请通过Revo Uninstaller将其卸载并安装最新的GA和已发布的 Web客户端版本。
  • 进行系统重启
  • 执行@Test

答案 1 :(得分:1)

真正的错误是unknown error: Chrome failed to start: exited abnormally

因为,您正在使用chromedriver=2.38.551591

请确保您是否使用了与本地计算机上安装的Chrome浏览器相关的正确的chromedriver版本。

您的chromedriver版本适用于 chrome浏览器版本&gt; 67.xx

您可以参考此page以获取兼容性参考。

答案 2 :(得分:0)

以下代码在同一环境下为我工作(但我的chromedriver版本是2.36):

options = Options()
options.add_experimental_option("detach", True)
options.add_argument("--window-position=0,0")
options.add_argument("--headless")
driver = webdriver.Chrome("path", chrome_options=options)

检查它是否适合您:)