【问题标题】:"http.client.CannotSendRequest: Request-sent" error“http.client.CannotSendRequest:请求发送”错误
【发布时间】:2016-02-26 05:09:38
【问题描述】:

这里有一个奇怪的问题。我有一个 24/7 运行的 Python 3 脚本,并使用 Selenium 和 Firefox 访问网页,每 5 分钟从下载链接下载一个文件(我不能只使用 urllib 或其他方式下载该文件,因为即使下载文件的链接地址保持不变,文件中的数据不断变化,每次重新加载页面时都会有所不同,也取决于指定的标准)。该脚本几乎一直运行良好,但我无法摆脱这个偶尔弹出的错误,该错误会终止脚本。这是错误:

Traceback (most recent call last):
  File "/Users/Shared/ROTH_1/Folio/get_F_notes.py", line 248, in <module>
    driver.get(search_url)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 187, in get
    self.execute(Command.GET, {'url': url})
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 173, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/remote_connection.py", line 349, in execute
    return self._request(command_info[0], url, body=data)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium/webdriver/remote/remote_connection.py", line 379, in _request
    self._conn.request(method, parsed_url.path, body, headers)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1090, in request
    self._send_request(method, url, body, headers)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1118, in _send_request
    self.putrequest(method, url, **skips)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 966, in putrequest
    raise CannotSendRequest(self.__state)
http.client.CannotSendRequest: Request-sent

这是我的脚本中出现问题的部分,具体来说,脚本命中了“除了 ConnectionRefusedError:”部分,并按预期打印出“警告 1:ConnectionRefusedError:搜索页面未加载;现在正在尝试再次”。但是,我认为,当循环再次开始并再次尝试“driver.get(search_url)”时,我得到了上述错误。脚本在这一点上窒息并给了我上述错误。

我对此进行了相当多的研究,似乎该脚本试图重用第一次尝试时的相同连接。修复似乎是创建一个新的连接。但这就是我所能收集到的全部信息,而且我不知道如何与 Selenium 建立新的联系。你?或者,这里还有其他问题吗?

search_url = 'https://www.example.com/download_page'
loop_get_search_page = 1
while loop_get_search_page < 7:
    if loop_get_search_page == 6:
        print('WARNING: tried to load search page 5 times; exiting script to try again later')
        ##### log out
        try:
            driver.find_element_by_link_text('Sign Out')
        except NoSuchElementException:
            print('WARNING: NoSuchElementException: Unable to find the link text for the "Sign Out" button')
        driver.quit()
        raise SystemExit
    try:
        driver.get(search_url)
    except TimeoutException:
        print('WARNING ', loop_get_search_page, ': TimeoutException: search page did not load; now trying again', sep='')
        loop_get_search_page += 1
        continue
    except ConnectionRefusedError:
        print('WARNING ', loop_get_search_page, ': ConnectionRefusedError: search page did not load; now trying again')
        loop_get_search_page += 1
        continue
    else:
        break

【问题讨论】:

    标签: python firefox selenium


    【解决方案1】:

    我自己也遇到了这个问题。就我而言,我有另一个线程在运行,它也通过 WebDriver 发出请求。原来 WebDriver 是 not 线程安全的。

    查看Can Selenium use multi threading in one browser? 上的讨论以及那里的链接以获取更多上下文。

    当我删除另一个线程时,一切都开始按预期工作。

    是否有可能每 5m 运行一个新线程?

    我所知道的“创建新连接”的唯一方法是启动 WebDriver 的新实例。如果您执行大量请求,这可能会变慢,但是由于您每 5m 才执行一次操作,因此它不应该真正影响您的吞吐量。只要您在 dl 完成后始终清理 WebDriver 实例,这对您来说可能是一个不错的选择。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 2014-11-26
      • 2023-03-20
      相关资源
      最近更新 更多