「Selenium」- Can not connect to the Service /path/to/chromedriver @20210211

在尝试使用Python的Selenium库运行Chrome浏览器时遇到了'Cannot connect to the Service'错误,具体为'selenium.common.exceptions.WebDriverException'。问题根源在于ChromeDriver与Selenium之间的连接失败。解决方案是将127.0.1.1 localhost添加到/etc/hosts文件中。确保ChromeDriver的路径正确无误,并且与Selenium版本匹配。

问题描述

# python3.7 /tmp/demo.py
Traceback (most recent call last):
  File "/tmp/demo.py", line 4, in <module>
    driver = webdriver.Chrome('/srv/sharing/packages/chromedriver_linux64/chromedriver')  # Optional argument, if not specified will search path.
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/common/service.py", line 104, in start
    raise WebDriverException("Can not connect to the Service %s" % self.path)
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service /srv/sharing/packages/chromedriver_linux64/chromedriver

问题原因

Selenium python: Can not connect to the Service %s" % self.path
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service geckodriver

解决办法

添加 127.0.1.1 localhost 到 /etc/hosts 文件。

相关文章

「Selenium Grid 3」- Node xxxxx has no free slots(close() vs quit())
「Selemium」- ChromeDriver only supports characters in the BMP

参考文献

Selenium python: Can not connect to the Service %s" % self.path
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service geckodriver

from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options options = Options() options.add_argument('--headless') # 可选:无头模式 options.add_argument('--disable-gpu') driver = webdriver.Chrome( service=Service("/usr/local/bin/chromedriver"), options=options ) driver.get("https://www.google.com") print(driver.title) driver.quit() --------------------------------------------------------------------------- WebDriverException Traceback (most recent call last) Cell In[12], line 9 6 options.add_argument('--headless') # 可选:无头模式 7 options.add_argument('--disable-gpu') ----> 9 driver = webdriver.Chrome( 10 service=Service("/usr/local/bin/chromedriver"), 11 options=options 12 ) 14 driver.get("https://www.google.com") 15 print(driver.title) File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/selenium/webdriver/chrome/webdriver.py:45, in WebDriver.__init__(self, options, service, keep_alive) 42 service = service if service else Service() 43 options = options if options else Options() ---> 45 super().__init__( 46 browser_name=DesiredCapabilities.CHROME["browserName"], 47 vendor_prefix="goog", 48 options=options, 49 service=service, 50 keep_alive=keep_alive, 51 ) File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/selenium/webdriver/chromium/webdriver.py:55, in ChromiumDriver.__init__(self, browser_name, vendor_prefix, options, service, keep_alive) 52 options.browser_version = None 54 self.service.path = finder.get_driver_path() ---> 55 self.service.start() 57 executor = ChromiumRemoteConnection( 58 remote_server_addr=self.service.service_url, 59 browser_name=browser_name, (...) 62 ignore_proxy=options._ignore_local_proxy, 63 ) 65 try: File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/selenium/webdriver/common/service.py:109, in Service.start(self) 107 count += 1 108 if count == 70: --> 109 raise WebDriverException(f"Can not connect to the Service {self._path}") WebDriverException: Message: Can not connect to the Service /usr/local/bin/chromedriver
06-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值