【问题标题】:How to launch Chrome in Selenium correctly如何在 Selenium 中正确启动 Chrome
【发布时间】:2017-11-08 04:27:55
【问题描述】:

我无法在 Selenium 中启动 Chrome。

driver=webdriver.Chrome()

Traceback(最近一次调用最后一次):

  File "<stdin>", line 1, in <module>

  File "/usr/anaconda/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 67, in __init__
    desired_capabilities=desired_capabilities)

  File "/usr/anaconda/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 87, in __init__
    self.start_session(desired_capabilities, browser_profile)

  File "/usr/anaconda/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 141, in start_session
    'desiredCapabilities': desired_capabilities,
  File "/usr/anaconda/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
    self.error_handler.check_response(response)

  File "/usr/anaconda/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
  (Driver info: chromedriver=2.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4),platform=Linux 3.19.8-100.fc20.x86_64 x86_64)

我使用“chromedriver_linux64.zip 2017-10-03 21:09:52 3.90MB” url.

【问题讨论】:

  • 你解压了 chromedriver_linux64.zip 吗?
  • @Manmohan_singh 当然!

标签: python google-chrome selenium


【解决方案1】:

您正在使用当前最新的chromedriver 2.33Google Chrome 38.0.2125.104

来自release notes,对该版本的支持是:

----------ChromeDriver v2.33 (2017-10-03)----------
Supports Chrome v60-62

还要确保您使用的是最新的稳定版 selenium。

此外,来自Help WebDriver find the downloaded ChromeDriver executable,你应该做其中之一

  1. 在 PATH 环境变量中包含 ChromeDriver 位置
  2. (仅限 Python)在实例化 webdriver.Chrome 时包含 ChromeDriver 的路径(参见下面的示例)

【讨论】:

  • 我可以通过 driver = webdriver.Chrome('/usr/bin/google-chrome-stable') 启动 Chrome。但过了一会儿,Traceback(最近一次调用最后):文件“”,第 1 行,在 文件“/usr/anaconda/lib/python2.7/site-packages/selenium/webdriver/chrome/ webdriver.py”,第 61 行,在 init self.service.start() 文件“/usr/anaconda/lib/python2.7/site-packages/selenium/webdriver/chrome/service.py ",第 88 行,在 start os.path.basename(self.path) + "'") selenium.common.exceptions.WebDriverException:消息:无法连接到 'google-chrome-stable'
【解决方案2】:

如果您要使用旧版本的 Chrome - 您需要匹配 chromedriver 的版本。在您的情况下,您使用的是 Chrome 38,它最后一次由 ChromeDriver 2.13 正式支持,您可以找到 here

如果您的用户使用的是较新版本的 Chrome,我建议您更新您的发行版和安装的 chrome 以匹配他们。

【讨论】:

    【解决方案3】:

    错误说明了一切:

    File "<stdin>", line 1, in <module>
    

    似乎错误发生在第一行,即:

    driver=webdriver.Chrome()
    

    这是因为在此代码块中,除非您导入webdriver,否则driver 对象无法正确启动/处理WebBrowser 的实例,即Chrome Browser。。 p>

    解决办法:

    在使用 Selenium 3.xChromeDriver 2.33.x 时,您需要 Chrome Browser v60-62Python 3.x 绑定,并且您必须执行以下操作:

    • this link 下载chromedriver 二进制文件。
    • 导入必要的模块
    • 提供系统中chromedriver 二进制文件的绝对路径。
    • 以下是最小代码:

        //The Linux Example
        from selenium import webdriver
        driver = webdriver.Chrome(executable_path=r'/usr/bin/chromedriver')
        driver.get('https://www.google.co.in')
        print("Page Title is : %s" %driver.title)
        driver.quit()
    

        //The Windows Example
        from selenium import webdriver
        driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
        driver.get('https://www.google.co.in')
        print("Page Title is : %s" %driver.title)
        driver.quit()
    

    更新:

    如您所见,在单独的注释中 WebDriverException: Message: unknown error: Chrome failed to start: crashed 执行以下附加步骤:

    • 通过Revo Uninstaller从您的系统中卸载Google Chrome
    • 运行 CCleaner 以清除所有不需要的 OS 杂务。
    • 参加system Reboot
    • 安装Google Chrome的最新官方版本
    • 执行您的 Test

    【讨论】:

      【解决方案4】:

      导入 selenium 和 chrome.option 让 chrom 在无头模式下运行

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

      如果您在 linux 上使用 chrome 或 chromiun 作为 root 用户,您应该添加“--no-sandbox”选项并设置窗口大小以避免某些项目因为窗口大小太小而无法显示

      chrome_options = Options()
      chrome_options.add_argument("--headless") # headless mode
      chrome_options.add_argument("--no-sandbox") # run as root user should add --no-sandbox option
      chrome_options.add_argument("--window-size=1920x1080")
      

      你可以从http://chromedriver.storage.googleapis.com下载chrome驱动,最新版本是2.38

      chrome_driver = "path to chromedriver"
      # start the driver 
      driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
      driver.get("https://www.google.com")
      

      【讨论】:

      • 请在答案中提供一些解释:)
      • 我通过 apt-get install chromium-browser 安装 chromium
      猜你喜欢
      • 1970-01-01
      • 2015-11-19
      • 2021-02-26
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      相关资源
      最近更新 更多