python - selenium.common.exceptions.WebDriverException : Message: Can not connect to the Service error using ChromeDriver Chrome through Selenium Python

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

所以我在一台计算机上使用 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: Can not connect to the Service C:\chromedriver.exe

我不确定问题出在哪里。

顺便说一下,我正在使用 windows。

编辑:我尝试过但没有奏效的事情:

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

2- 重新安装 chrome

3- 使用 beta-chroma 80 和 webdriver 80

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

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

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

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

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

9- 运行服务测试

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

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

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

最佳答案

这个错误信息...

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

...暗示 ChromeDriver 无法启动/生成新的浏览上下文,即 Chrome 浏览器 session 。


您需要处理几件事:

  • 确保您已从 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 Client 版本太旧,则将其卸载并安装最新的 GA 和发布版本的 Web Client
  • 系统重启
  • 非root用户身份执行您的@Test

引用资料

您可以在以下位置找到一些引用讨论:

关于python - selenium.common.exceptions.WebDriverException : Message: Can not connect to the Service error using ChromeDriver Chrome through Selenium Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59532730/

相关文章:

python 导入语句导致 NoneType

python - 如何使用 scrapy 在两个连续请求中进行回调

c# - 如何从 Chrome 驱动程序(Selenium Web 驱动程序 C#)中分离 Chrome 浏览器

java - 面临 HtmlUnitDriver 的问题

google-chrome - 可以使用 Puppeteer 从 headless chrome 以编程方式生成 har 文件吗?

python - 我的数据集显示一个字符串,而它应该是一个大括号集/字典

python - tkinter 与 glib mainloop 的集成

javascript - Chrome扩展能否动态添加JS文件到本地扩展目录?用户上传还是从网络保存?

css - 为什么我不能将 selenium python 中的点击发送到元素?

ajax - Google Chrome 将 JSON AJAX 响应显示为树而不是纯文本