我试图用一个URL启动chrome浏览器,浏览器启动后,它什么也不做。

1分钟后我看到如下错误:

Unable to open browser with url: 'https://www.google.com' (Root cause: org.openqa.selenium.WebDriverException: unknown error: DevToolsActivePort file doesn't exist
  (Driver info: chromedriver=2.39.562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9),platform=Windows NT 10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information)

我的配置:

Chrome浏览器:66 ChromeBrowser: 2.39.56

又及,在Firefox中一切都很好


当前回答

我有同样的问题,但在我的情况下,chrome之前安装在用户临时文件夹,之后被重新安装到程序文件。所以这里提供的任何解决方案都帮不了我。但是如果提供了chrome.exe的路径就可以了:

chromeOptions.setBinary("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");

我希望这能帮助到一些人=)

其他回答

以我为例,在以下环境中:

Windows 10 Python 3.7.5 谷歌Chrome 80版本及对应的ChromeDriver路径为“C:\Windows” 硒3.141.0

我需要将参数——no-sandbox和——remote-debug -port=9222添加到ChromeOptions对象中,并以管理员用户身份运行Powershell/cmd来运行代码。

下面是相关的代码段:

options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('--disable-infobars')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-sandbox')
options.add_argument('--remote-debugging-port=9222')
driver = webdriver.Chrome(options=options)

更新conf.js中的功能

exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',
  specs: ['todo-spec.js'],
  capabilities: {
    browserName: 'chrome',
    chromeOptions: {
      args: ['--disable-gpu', '--no-sandbox', '--disable-extensions', '--disable-dev-shm-usage']
    }
  },

};

没有解决方法对我有效。但这里有一个变通办法:

maxcounter=5
for counter in range(maxcounter):
    try:           
        driver = webdriver.Chrome(chrome_options=options,
                          service_log_path=logfile,
                          service_args=["--verbose", "--log-path=%s" % logfile])
        break
    except WebDriverException as e:
        print("RETRYING INITIALIZATION OF WEBDRIVER! Error: %s" % str(e))
        time.sleep(10)
        if counter==maxcounter-1:
            raise WebDriverException("Maximum number of selenium-firefox-webdriver-retries exceeded.")

我也经历过这个问题,提出的解决方案似乎都不起作用。然后我发现问题是我在WSL版本1上运行,似乎chromedriver与windows浏览器一起工作,而不是与aptitude安装的浏览器。

为了使它与WSL版本1和版本2兼容,我发现(并在debian WSLv2和ubuntu WSLv1中进行了测试,它可以工作),该平台的版本显示的单词Microsoft的第一个大写字母。

所以解决方案是这样的:

    import platform
    from selenium.webdriver.chrome.service import Service as ChromeService
    from seleniumwire import webdriver

    chromedriver = 'chromedriver.exe'
    
    # According to:
    # https://stackoverflow.com/a/71879688/7019069
    # When using WSL v1 the chromedriver.exe of local chrome of windows is used
    # In WSL v2 (updated version) it does work using the installation shown in the README.md)
    # And according to
    # https://github.com/microsoft/WSL/issues/4555
    # It is possible to differentiate the version of WSL by the first uppercase of the platform 
    # version of Microsoft. Therefore only the linux chromedriver is used if that word is matched 
    # in the platform of the driver. 
    if platform.system() == 'Linux' and not re.search(re.escape('Microsoft'), platform.platform()):
        chromedriver = 'chromedriver'

    driver_path = os.path.join(drivers_path, chromedriver)
  service = ChromeService(driver_path)

    driver = webdriver.Chrome(
        service=service,
        options=__get_chrome_options(headless)
    )

我使用铬,但我已经创建了一个shell脚本称为铬,只是为了便于我从dmenu打开浏览器。

#!/bin/bash

/usr/bin/chromium

Chrome驱动程序在PATH中寻找Chrome并执行。结果我得到了同样的错误。

org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited normally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /home/s1n7ax/.local/share/s1n7ax/bin/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 's1n7ax', ip: '127.0.1.16', os.name: 'Linux', os.arch: 'amd64', os.version: '5.4.70-1-lts', java.version: '11.0.8'
Driver info: driver.version: ChromeDriver
remote stacktrace: #0 0x56030c96dd99 <unknown>

我只是删除了shell脚本,并添加了一个到chromium的软链接。现在一切正常。