[英]selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service error using ChromeDriver Chrome through Selenium Python
So I have made a program on one computer using selenium and that worked, Now using it in another computer I get this error:所以我在一台计算机上使用 selenium 制作了一个程序并且工作正常,现在在另一台计算机上使用它我得到这个错误:
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver
selenium.common.exceptions.WebDriverException:消息:无法连接到服务 chromedriver
Now this same issue was mention in:现在在以下内容中提到了同样的问题:
Selenium python: Can not connect to the Service %s" % self.path Selenium python:无法连接到服务 %s" % self.path
Selenium python: Can not connect to the Service %s" % self.path Selenium python:无法连接到服务 %s" % self.path
Selenium and Python3 ChromeDriver raises Message: Can not connect to the Service chromedriver Selenium 和 Python3 ChromeDriver 引发消息:无法连接到服务 chromedriver
however the solutions mentioned didnt work.然而,提到的解决方案不起作用。
I am using chrome version 79 and have installed chromedriver 79, I tested writing chromedriver in command line and that works which means path is configured right, I have made sure 127.0.0.1 localhost is also in etc/hosts我正在使用 chrome 版本 79 并安装了 chromedriver 79,我测试了在命令行中编写 chromedriver 并且可以正常工作,这意味着路径配置正确,我确保 127.0.0.1 localhost 也在 etc/hosts 中
Below is my code which works on my computer (so i doubt its an issue with the code):下面是我在我的电脑上运行的代码(所以我怀疑它的代码有问题):
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
In the last question I also tried this modification:在最后一个问题中,我也尝试了这种修改:
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
which would instead give me this almost identical error message:这反而会给我这个几乎相同的错误信息:
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service C:\\chromedriver.exe
selenium.common.exceptions.WebDriverException:消息:无法连接到服务 C:\\chromedriver.exe
I am unsure where the issue could lie.我不确定问题可能出在哪里。
I am using windows by the way.顺便说一下,我正在使用Windows。
EDIT: Things I tried and didn't work:编辑:我尝试过但没有奏效的事情:
1- running everything as both admin and normal 1- 以管理员和普通身份运行一切
2- re-installing chrome 2-重新安装铬
3- using the beta-chroma 80 and webdriver 80 3- 使用 beta-chroma 80 和 webdriver 80
4- using normal chrome 79 and webdriver 79 4- 使用普通的 chrome 79 和 webdriver 79
5- Having both the script and the driver in the same directory (while using a correct path) 5- 将脚本和驱动程序放在同一目录中(同时使用正确的路径)
6- Having an external path and have it setup as needed 6- 拥有外部路径并根据需要进行设置
7- Using it in the PATH folder. 7- 在 PATH 文件夹中使用它。
8- Adding "127.0.0.1 localhost" to etc/hosts 8- 将“127.0.0.1 localhost”添加到 etc/hosts
9- Running service test 9- 运行服务测试
I have ensured in every test that everything was in it's correct placement, I have ran a reboot before every new test, and they always give me the same error, which also happens to occur if I had an incorrect path as well, but once I ran the code for a service and it gave me a different error as I had my webdriver in C:/ which required admin privilages, however re-running the test again with the correct privilages gave back the same error我在每次测试中都确保一切都在正确的位置,我在每次新测试之前都进行了重新启动,他们总是给我同样的错误,如果我的路径不正确也会发生这种情况,但是一旦我运行服务的代码,它给了我一个不同的错误,因为我在 C:/ 中有我的网络驱动程序,这需要管理员权限,但是使用正确的权限再次重新运行测试返回了相同的错误
Update the issue isn't exclusive to the chrome driver.更新问题并非 chrome 驱动程序独有。 Even following setup instructions for either the Firefox or edge drivers end up on the same issues.
即使遵循 Firefox 或 Edge 驱动程序的设置说明,最终也会遇到相同的问题。 It makes me suspect that the connection is facing some issue.
这让我怀疑连接正面临一些问题。 I have tried running the test codes provided by Mozilla for the setup and it didn't work.
我试过运行 Mozilla 提供的测试代码进行设置,但没有奏效。
Unsure if that does help much or at all.不确定这是否有很大帮助或根本没有帮助。
This error message...这个错误信息...
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver
...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context ie Chrome Browser session. ...暗示ChromeDriver无法启动/生成新的浏览上下文,即Chrome 浏览器会话。
You need to take care of a couple of things:您需要注意以下几点:
Ensure that you have downloaded the exact format of the ChromeDriver binary from the download location pertaining to your underlying OS among:确保您已从与您的底层操作系统相关的下载位置下载了准确格式的ChromeDriver二进制文件:
Ensure that /etc/hosts
file contains the following entry:确保
/etc/hosts
文件包含以下条目:
127.0.0.1 localhost
Ensure that ChromeDriver binary have executable permission for the non-root user.确保ChromeDriver二进制文件对非 root用户具有可执行权限。
Ensure that you have passed the proper absolute path of ChromeDriver binary through the argument executable_path
as follows:确保您已通过参数
executable_path
传递ChromeDriver二进制executable_path
的正确绝对路径,如下所示:
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
Finally, to avoid incompatibility between the version of the binaries you are using ensure that:最后,为避免您使用的二进制文件版本之间的不兼容,请确保:
@Test
as non-root user.@Test
。You can find a couple of reference discussions in:您可以在以下位置找到一些参考讨论:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.