【问题标题】:How to fix "selenium.common.exceptions.WebDriverException: Message: Failed to write request to stream" with python如何使用 python 修复“selenium.common.exceptions.WebDriverException: Message: Failed to write request to stream”
【发布时间】:2021-09-30 08:45:35
【问题描述】:

环境:

Python 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 中搜索此错误消息,但找不到任何内容。

有人可以帮帮我吗?

【问题讨论】:

  • 能否提供网站链接或显示HTML代码?
  • 该网站是 Reddit 主页 reddit.com 我想定位登录按钮,我点击错了,Xpath 不好我再次给这行(我放弃了 Xpath 的一部分)@ 987654325@ button_LogIn = WebDriverWait(p_driver, 10).until(EC.presence_of_element_located((By.XPATH, ("//a[contains(@href,'reddit.com/login')]")))) code

标签: python python-3.x selenium


【解决方案1】:

您使用的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()

【讨论】:

  • Xpath 有同样的问题:-S raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: Failed to write request to stream
猜你喜欢
  • 2021-07-30
  • 1970-01-01
  • 2021-09-13
  • 2021-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多