selenium.common.exceptions.WebDriverException: Message: 使用ChromeDriver和Selenium时出现“chrome not reachable”错误,而使用find_element_by_id方法。

7
这是HTML代码:

在这里

< input class="form-control input-lg input auto-complete" id="ymMsgInput" type="text" placeholder="Type your message ..." autocomplete="off" >

代码:

i = s.find_element_by_id("ymMsgInput");

Python - Selenium Chrome web driver 出现错误:

Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    i = s.find_element_by_id("ymMsgInput");
  File "C:\Users\vishn\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 351, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "C:\Users\vishn\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 955, in find_element
    'value': value})['value']
  File "C:\Users\vishn\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "C:\Users\vishn\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: chrome not reachable
  (Session info: chrome=65.0.3325.146)
  (Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 10.0.16299 x86_64)
3个回答

10

错误信息已经说得很清楚了:

    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: chrome not reachable

如果用户使用的二进制文件不兼容,则会出现此错误,但这绝对不是您的情况,因为您正在:

但是,自从最新的Chromedriver 2.36发布以来,Selenium用户一直遇到问题。以下是其中一个线程:

根本原因与提交相关:

  • 删除--disable-infobars

    因此,有几种可能的解决方案:

  • 使用ChromeOptions类来最大化浏览器。

  • 删除选项disable-infobars
  • 例如:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument("start-maximized")
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe')
    driver.get('https://www.google.co.in')
    print("Page Title is : %s" %driver.title)
    

如果您的问题仍然存在,请考虑以下内容:

  • Selenium Python客户端升级到当前版本3.10.0版本
  • ChromeDriver升级到稳定版本 ChromeDriver v2.35 级别。
  • Chrome版本保持在Chrome v64.x级别。(根据ChromeDriver v2.35发布说明
  • 通过您的IDE清理项目工作区并仅使用所需依赖项重新构建您的项目。
  • 使用CCleaner工具在执行测试套件之前和之后擦除所有操作系统任务。
  • 如果您的基本Chrome版本太旧,则通过Revo Uninstaller卸载它,并安装最新的GA和发布版Chrome。
  • 执行您的测试

9

你的异常不是关于查找元素。Selenium 无法联系 Chrome。你可以做几件事情。

  1. Downgrade/upgrade your chromedriver based on your selenium version.

  2. Pass --no-sandbox to chrome options.

    chrome_options.add_argument('--no-sandbox')
    chrome = webdriver.Chrome('/usr/local/bin/chromedriver', chrome_options=chrome_options)
    

0
除了上面提到的版本问题(例如,使用pip uninstall chromedriver-binary卸载并使用pip install chromedriver-binary==104.0.5112.20安装不同版本):
在Python中运行“driver = webdriver.Chrome()”后,请勿关闭浏览器窗口!关闭它将干扰后续使用driver.get()的代码行。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接