【问题标题】:selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service error using ChromeDriver Chrome through Selenium Pythonselenium.common.exceptions.WebDriverException:消息:无法通过 Selenium Python 使用 ChromeDriver Chrome 连接到服务错误
【发布时间】:2019-12-30 14:53:38
【问题描述】:

所以我在一台计算机上使用 selenium 制作了一个程序并且可以正常工作,现在在另一台计算机上使用它时出现此错误:

selenium.common.exceptions.WebDriverException:消息:无法连接到服务 chromedriver

现在同样的问题被提及:

Selenium python: Can not connect to the Service %s" % self.path

Selenium python: Can not connect to the Service %s" % self.path

Selenium and Python3 ChromeDriver raises Message: Can not connect to the Service chromedriver

但是提到的解决方案不起作用。

我正在使用 chrome 版本 79 并安装了 chromedriver 79,我测试了在命令行中编写 chromedriver,这意味着路径配置正确,我确保 127.0.0.1 localhost 也在 etc/hosts 中

下面是在我的电脑上运行的代码(所以我怀疑它是代码的问题):

chrome_options = Options()   
chrome_options.add_argument("--headless")  
with webdriver.Chrome(chrome_options=chrome_options) as driver:
    driver.set_window_size(800, 460) # takes two arguments, width and height of the browser and it has to be called before using get()
    driver.execute_script("document.body.style.zoom='150%'")
    driver.get("file:\\"+url) # takes one argument, which is the url of the website you want to open
    driver.find_element_by_tag_name('body').screenshot(output)  # avoids scrollbar

上一个问题我也试过这个修改:

chrome_options = Options()   
chrome_options.add_argument("--headless")  
with webdriver.Chrome("C:\\chromedriver.exe",chrome_options=chrome_options) as driver:
    driver.set_window_size(800, 460) # takes two arguments, width and height of the browser and it has to be called before using get()
    driver.execute_script("document.body.style.zoom='150%'")
    driver.get("file:\\"+url) # takes one argument, which is the url of the website you want to open
    driver.find_element_by_tag_name('body').screenshot(output)  # avoids scrollbar

这反而会给我这个几乎相同的错误消息:

selenium.common.exceptions.WebDriverException:消息:无法连接到服务 C:\chromedriver.exe

我不确定问题出在哪里。

顺便说一句,我正在使用 Windows。

编辑:我尝试过但没有成功的方法:

1- 以管理员和正常方式运行所有内容

2-重新安装chrome

3- 使用 beta-chroma 80 和 webdriver 80

4- 使用普通的 chrome 79 和 webdriver 79

5- 将脚本和驱动程序放在同一目录中(使用 正确的路径)

6- 拥有外部路径并根据需要进行设置

7- 在 PATH 文件夹中使用它。

8- 向 etc/hosts 添加“127.0.0.1 localhost”

9- 运行服务测试

我已确保在每次测试中一切都在正确的位置,我在每次新测试之前都进行了重新启动,他们总是给我同样的错误,如果我的路径也不正确,也会发生这种情况,但是一旦我运行了服务的代码,它给了我一个不同的错误,因为我在 C:/ 中有我的 webdriver,需要管理员权限,但是使用正确的权限再次重新运行测试会返回相同的错误

更新此问题并非 Chrome 驱动程序独有。即使遵循 Firefox 或边缘驱动程序的设置说明最终也会遇到相同的问题。这让我怀疑连接正面临一些问题。我已尝试运行 Mozilla 为设置提供的测试代码,但没有成功。

不确定这是否有很大帮助或根本没有帮助。

【问题讨论】:

  • 您的chromedriver.exe 文件是否存储在C:\chromedriver.exe
  • 是的,它存储在 C:\ 以及我的 PATH 目录中
  • 尝试自己启动 chrome 服务,并检查您是否可以使用类似 localhost:port 的方式在浏览器中访问它。启动 chrome 服务的示例 ` service = Service('/path/to/chromedriver') service.start() driver = webdriver.Remote(service.service_url) ` chromedriver.chromium.org/getting-started 。如果访问服务 url 有问题,则可能与防火墙有关
  • 让我尽快测试一下。
  • 尝试使用webdriver.ChromeOptions() 而不是Options()

标签: python selenium google-chrome selenium-webdriver selenium-chromedriver


【解决方案1】:

此错误消息...

selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver

...暗示 ChromeDriver 无法启动/产生新的 Browsing ContextChrome Browser 会话。


你需要注意几件事:

  • 确保您已从download location 下载了与您的底层操作系统相关的 ChromeDriver 二进制文件的确切格式:

    • chromedriver_linux64.zip:适用于 Linux 操作系统
    • chromedriver_mac64.zip:适用于 Mac OSX
    • chromedriver_win32.zip:适用于 Windows 操作系统
  • 确保/etc/hosts 文件包含以下条目:

    127.0.0.1 localhost 
    
  • 确保 ChromeDriver 二进制文件对 非 root 用户具有可执行权限。

  • 确保您已通过参数executable_path 传递了正确的 ChromeDriver 二进制文件的绝对路径,如下所示:

    with webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=chrome_options) as driver:
    
  • 所以你的有效代码块将是:

    options = Options()   
    options.add_argument("--headless")
    options.add_argument('--no-sandbox') # Bypass OS security model
    options.add_argument('--disable-gpu')  # applicable to windows os only
    options.add_argument("--disable-dev-shm-usage")  # overcome limited resource problems
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    with webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', options=options) as driver:
        driver.set_window_size(800, 460) # takes two arguments, width and height of the browser and it has to be called before 
        driver.execute_script("document.body.style.zoom='150%'")
        driver.get("file:\\"+url) # takes one argument, which is the url of the website you want to open
        driver.find_element_by_tag_name('body').screenshot(output)  # avoids scrollbar
    

强制性注意事项

最后,为避免您使用的二进制文件版本之间出现不兼容,请确保:

  • Selenium 升级到当前级别 Version 3.141.59
  • ChromeDriver 已更新到当前的ChromeDriver v79.0.3945.36 级别。
  • Chrome 已更新至当前 Chrome 版本 79.0 级别。 (根据ChromeDriver v79.0 release notes
  • 清理你的项目工作区通过你的IDE重建你的项目只需要依赖。
  • 如果您的基本 Web 客户端 版本太旧,请卸载它并安装最新的 GA 和发布版本的 Web 客户端
  • 进行系统重启
  • 非root用户身份执行@Test

参考文献

您可以在以下位置找到一些参考讨论:

【讨论】:

  • 它仍然无法正常工作,我还测试了将服务作为测试运行的建议,它给了我同样的问题,也不确定它是否意义重大,但需要一段时间出现该错误。同时程序似乎卡住了。
  • @ZaidAlShatle 您是否尝试将 chromedriver.exeC: 替换为 C:\some_directory 并以 non-administrator 用户身份执行测试并添加 argument i> 我在回答中提到过?
  • @ZaidAlShatle 是否将关键字executable_path 添加到chromedriver 二进制文件C:\some_directory 的绝对路径的值中?单个正斜杠,即 (\ ),前面是原始开关,即 r?
  • 第二个试过了,没试过从C:/改目录。我会试试的。
  • 尝试打开您的 chrome 浏览器。几分钟前我在使用 Django 时遇到了这个问题,我通过更改 Django 的端口并实际打开 chrome 浏览器来修复它。由于某些我不明白的原因,如果我不这样做,它无法自行打开浏览器。
猜你喜欢
  • 2020-04-16
  • 2019-10-22
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
  • 2017-04-24
  • 2019-06-16
相关资源
最近更新 更多