首页 > 解决方案 > 如何使用 python 修复“selenium.common.exceptions.WebDriverException: Message: Failed to write request to stream”

问题描述

环境:

蟒蛇 3.9.7

硒==3.141.0

问题:

我正在尝试使用 selenium 在网站上获取元素。

button_LogIn = WebDriverWait(p_driver, 10).until(EC.presence_of_element_located(
        (By.XPATH, ("//a[contains(@href,'https://www.reddit.com/login'"))))

但我收到此错误消息:

Traceback (most recent call last):
  File "C:\Users\mmetral\Desktop\PhoneBot\PhoneBotV3\modules\robots\reddit\Reddit_Browser_Bot.py", line 510, in <module>
    RunRedditBrowser(p_browser, p_driver, p_function, p_taskuser_id,label_log,lock)
  File "C:\Users\mmetral\Desktop\PhoneBot\PhoneBotV3\modules\robots\reddit\Reddit_Browser_Bot.py", line 441, in RunRedditBrowser
    AreWeRedditLoggedIn(p_driver)
  File "C:\Users\mmetral\Desktop\PhoneBot\PhoneBotV3\modules\robots\reddit\Reddit_Browser_Bot.py", line 193, in AreWeRedditLoggedIn
    button_LogIn = WebDriverWait(p_driver, 10).until(EC.presence_of_element_located(
  File "C:\Users\mmetral\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
    value = method(self._driver)
  File "C:\Users\mmetral\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 64, in __call__
    return _find_element(driver, self.locator)
  File "C:\Users\mmetral\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 415, in _find_element
    raise e
  File "C:\Users\mmetral\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 411, in _find_element
    return driver.find_element(*by)
  File "C:\Users\mmetral\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\mmetral\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\mmetral\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Failed to write request to stream


Process finished with exit code 1

我在 google 中搜索此错误消息,但找不到任何内容。

有人可以帮助我吗?

标签: pythonpython-3.xselenium

解决方案


你使用的xpath是错误的。尝试以下操作,按钮将被单击。

button_LogIn = WebDriverWait(p_driver, 10).until(EC.presence_of_element_located(
        (By.XPATH, ("//*[@id='SHORTCUT_FOCUSABLE_DIV']/div[1]/header/div/div[2]/div/div[1]/a[1]"))))

# clicking the button
button_LogIn.click()

推荐阅读