我使用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
吗? - crookedleafwebdriver.ChromeOptions()
代替Options()
。 - PiAreSquared