selenium.common.exceptions.WebDriverException: Message: 无法通过Selenium Python使用ChromeDriver Chrome连接服务错误。

4

我使用selenium在一台计算机上编写了一个程序,并且它可以正常工作。但是,在另一台计算机上使用时,我遇到了这个错误:

selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service 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: Message: 无法连接服务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-运行服务测试

在每个测试中,我都确保所有内容都放置在正确的位置,每次进行新测试之前都会重新启动计算机,但它们总是给我相同的错误,如果路径不正确也会发生此错误,但是当我运行服务代码时,它给出了不同的错误,因为我的webdriver在C:/中需要管理员权限,但是使用正确的权限再次运行测试仍然会出现相同的错误消息。

更新:问题不仅限于Chrome驱动程序。即使按照Firefox或Edge驱动程序的设置说明进行设置,也会遇到同样的问题。这使我怀疑连接面临某些问题。我尝试运行Mozilla提供的设置测试代码,但它没有起作用。

不确定这是否有所帮助。


你的 chromedriver.exe 文件存储在 C:\chromedriver.exe 吗? - crookedleaf
是的,它存储在C:\和我的PATH目录中。 - Zaid Al Shattle
尝试自己启动Chrome服务,并检查是否能够使用类似http://localhost:port的浏览器访问它。启动Chrome服务的示例:`service = Service('/path/to/chromedriver') service.start() driver = webdriver.Remote(service.service_url)` https://chromedriver.chromium.org/getting-started。如果访问服务URL存在问题,则可能与防火墙有关。 - Rahul L
让我很快测试一下。 - Zaid Al Shattle
尝试使用webdriver.ChromeOptions()代替Options() - PiAreSquared
1个回答

3

这个错误信息表明你的电脑无法连接到互联网。

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

这意味着ChromeDriver无法启动/生成新的浏览上下文,即Chrome浏览器会话。


您需要注意以下几点:

  • Ensure that you have downloaded the exact format of the ChromeDriver binary from the download location pertaining to your underlying OS among:

    • chromedriver_linux64.zip: For Linux OS
    • chromedriver_mac64.zip: For Mac OSX
    • chromedriver_win32.zip: For Windows OS
  • Ensure that /etc/hosts file contains the following entry:

    127.0.0.1 localhost 
    
  • Ensure that ChromeDriver binary have executable permission for the non-root user.

  • Ensure that you have passed the proper absolute path of ChromeDriver binary through the argument executable_path as follows:

    with webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=chrome_options) as driver:
    
  • So your effective code block will be:

    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 Version 79.0。 (根据ChromeDriver v79.0发布说明)
  • 通过你的 IDE 清理你的 项目工作空间 并仅使用所需的依赖项重建你的项目。
  • 如果你的基础 Web客户端 版本过旧,则卸载它并安装一个最近的GA和发布版本的 Web客户端
  • 进行一次 系统重启
  • 非root用户身份执行你的@Test

参考资料

你可以在以下讨论中找到一些参考:


仍然无法正常工作,我也尝试了将服务作为测试运行的建议,但仍然出现相同的问题。同时,不确定是否有任何意义,但是错误需要一段时间才会出现,而程序似乎卡住了。 - Zaid Al Shattle
@ZaidAlShattle 你尝试过将 chromedriver.exeC: 替换为 C:\some_directory,并以 非管理员 用户身份执行你的测试,添加我在答案中提到的 参数 吗? - undetected Selenium
@ZaidAlShattle 你是否将关键字 executable_path 添加到 chromedriver 二进制文件的绝对路径值 C:\some_directory 中?单个正斜杠,即 (\) 前面加上原始开关,即 r - undetected Selenium
我已经尝试了第二个,但我还没有尝试将目录从C:/更改。我会尝试一下。 - Zaid Al Shattle
1
尝试打开你的Chrome浏览器。我在使用Django时遇到了这个问题,几分钟前我通过更改Django的端口并实际打开Chrome浏览器来解决了它。由于某些我不理解的原因,它不能自己打开浏览器,必须由我手动打开。 - blockhead
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接