提问者:小点点

http. client.RemoteDisconnect:使用Selenium的driver.不响应错误的远程端关闭连接Python


我正在使用:

  • Python3.8
  • 硒3.141.0
  • Windows 10(在代理后面)
  • Chrome: 84.0.4147.105
  • 镀铬河: 84.0.4147.30
  • Mac 10.15.6(没有代理)

这是我的代码:

from selenium import webdriver

driver = webdriver.Chrome("D:/webdriver/chromedriver.exe")
driver.get("https://github.com")
driver.quit()

执行river.不执行()时,异常引发:

Traceback (most recent call last):
  File "C:/Users/taiping/Desktop/data_test/selenium_test.py", line 5, in <module>
    driver.quit()
  File "D:\python3.8\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 158, in quit
    self.service.stop()
  File "D:\python3.8\lib\site-packages\selenium\webdriver\common\service.py", line 151, in stop
    self.send_remote_shutdown_command()
  File "D:\python3.8\lib\site-packages\selenium\webdriver\common\service.py", line 127, in send_remote_shutdown_command
    url_request.urlopen("%s/shutdown" % self.service_url)
  File "D:\python3.8\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "D:\python3.8\lib\urllib\request.py", line 525, in open
    response = self._open(req, data)
  File "D:\python3.8\lib\urllib\request.py", line 542, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "D:\python3.8\lib\urllib\request.py", line 502, in _call_chain
    result = func(*args)
  File "D:\python3.8\lib\urllib\request.py", line 1379, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "D:\python3.8\lib\urllib\request.py", line 1354, in do_open
    r = h.getresponse()
  File "D:\python3.8\lib\http\client.py", line 1332, in getresponse
    response.begin()
  File "D:\python3.8\lib\http\client.py", line 303, in begin
    version, status, reason = self._read_status()
  File "D:\python3.8\lib\http\client.py", line 272, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

但是这段代码在我的Macbook上没有错误。有什么问题吗?

更新时间:2020-08-05

我打开调试器,发现每个HTTPConnection对象都已经设置了系统范围的超文本传输协议代理。但是我没有在我的代码中显式设置任何选项。并且driver.都退出方法会将http://localhost:59717/shutdown发送到chrome执行退出。所以我猜测关闭url实际上是发送到代理服务器,而不是本地浏览器。

我尝试使用fiddler来检查请求信息。还有一个问题是,由于一些证书配置,我无法解码https请求。所以我将river. get()的参数更改为我公司的内部Web url。结果是:如果我关闭fiddler,RemoteDisconnect错误再次上升。如果我打开fiddler,所有工作。

发生了什么?我知道fiddler将代理设置为127.0.0.1:8888,所以我认为代理设置可能有问题。但我无法修复它。我猜chrome使用系统代理,因此github主页可以正确打开,但当向chrome发送关闭url时,请求对象不应该使用系统代理,但它确实使用了。

我说得对吗?如何解决这个问题?


共3个答案

匿名用户

此错误消息…

    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

…表示远程端没有响应,并且与远程端的连接已关闭。

语法上,您的代码块中没有错误。但是GitHub主页包含AJAX元素。

最有可能的情况是,当网页到达Document. readyStateequals"完成"时,AJAX调用仍然处于活动状态,这意味着与远程端的连接仍未完全建立。在这种情况下,当您调用river.退出()时,会引发上述错误。

>

  • 代码块:

    from selenium import webdriver
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get("https://github.com")
    driver.quit()
    

    控制台输出:

    DevTools listening on ws://127.0.0.1:61488/devtools/browser/62c450cf-7ed5-4205-bb9c-d7e50cd13173
    [2780:12796:0804/183946.157:ERROR:broker_win.cc(55)] Error reading broker pipe: The pipe has been ended. (0x6D)
    

    要更清晰地执行river.不执行(),您需要诱导WebDriverWait为Javascript和AJAX完成某些元素,然后调用river.不执行(),如下所示:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get("https://github.com")
    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h1[text()='Built for developers']")))
    driver.quit()
    

    您可以在以下链接中找到一些详细的讨论:

    • 我们是否有任何通用函数来检查页面是否已在Selenium中完全加载
    • SeleniumIEWebDriver仅在调试时工作
    • Selenium如何管理等待页面加载?

  • 匿名用户

    我已经打开了硒回收的问题

    我的解决方案是发送一个没有代理的请求。这是我的代码:

    from selenium import webdriver
    import http.client
    
    driver = webdriver.Chrome("D:/webdriver/chromedriver.exe")
    driver.get("https://github.com")
    # driver.quit()
    # use below to quit and clean.
    conn = http.client.HTTPConnection(driver.service.service_url.split("//")[1])
    conn.request("GET", "/shutdown")
    conn.close()
    del driver
    

    这工作得很好。没有异常和chromeriver. exe关闭。但我认为这不是最好的解决方案。

    匿名用户

    我的OS是linux我解决这个问题添加no_poroxy在我的情况下添加添加no_proxylocalhost和Intranet ipsno_proxy="127.0.0.1,localhost,10…*…