![](/img/trans.png)
[英]selenium.common.exceptions.WebDriverException: Message: invalid session id using Selenium with ChromeDriver and Chrome through Python
[英]selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service error using ChromeDriver Chrome through Selenium Python
所以我在一台计算机上使用 selenium 制作了一个程序并且工作正常,现在在另一台计算机上使用它我得到这个错误:
selenium.common.exceptions.WebDriverException:消息:无法连接到服务 chromedriver
现在在以下内容中提到了同样的问题:
Selenium python:无法连接到服务 %s" % self.path
Selenium python:无法连接到服务 %s" % self.path
Selenium 和 Python3 ChromeDriver 引发消息:无法连接到服务 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-重新安装铬
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:/ 中有我的网络驱动程序,这需要管理员权限,但是使用正确的权限再次重新运行测试返回了相同的错误
更新问题并非 chrome 驱动程序独有。 即使遵循 Firefox 或 Edge 驱动程序的设置说明,最终也会遇到相同的问题。 这让我怀疑连接正面临一些问题。 我试过运行 Mozilla 提供的测试代码进行设置,但没有奏效。
不确定这是否有很大帮助或根本没有帮助。
这个错误信息...
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver
...暗示ChromeDriver无法启动/生成新的浏览上下文,即Chrome 浏览器会话。
您需要注意以下几点:
确保您已从与您的底层操作系统相关的下载位置下载了准确格式的ChromeDriver二进制文件:
确保/etc/hosts
文件包含以下条目:
127.0.0.1 localhost
确保ChromeDriver二进制文件对非 root用户具有可执行权限。
确保您已通过参数executable_path
传递ChromeDriver二进制executable_path
的正确绝对路径,如下所示:
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
最后,为避免您使用的二进制文件版本之间的不兼容,请确保:
@Test
。您可以在以下位置找到一些参考讨论:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.