“ http.client.CannotSendRequest:请求发送”错误

Jef*_*f F 5 python firefox selenium

这里的问题很奇怪。我有一个运行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
Run Code Online (Sandbox Code Playgroud)

这是我的脚本中出现问题的部分,具体地说,脚本命中了“ 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
Run Code Online (Sandbox Code Playgroud)

toe*_*ugh 5

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

查看Can Selenium 在一个浏览器中使用多线程吗?以及那里的链接以获取更多上下文。

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

您是否有可能在新线程中每 5m 运行一次?

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