【发布时间】: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