【问题标题】:urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=49951): Max retries exceeded with url with Selenium and Pythonurllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=49951): 使用 Selenium 和 Python 的 url 超出了最大重试次数
【发布时间】:2019-10-30 05:11:46
【问题描述】:

我不知道如何单击此按钮。它具有图像和文本等标识符,但我不确定如何使用它们来发挥我的优势。

我尝试过使用 XPATH,我尝试过使用文本,我尝试过使用图片的链接,但我无法让它正常工作

我的代码:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Continue Watching"))).click()

按钮 HTML:

<button style="margin-top: 15px;
            width: 240px;
            height: 46px;
            background-color: #69b8d6;
            margin: 50px auto;
            border-radius: 4px;
            color: white;
            display: block;
            margin-left: 159px;
            font-size: 16px;"><img style="padding-right:15px;" src="http://img.encrave.tv/global/watchCamcorder.png">Continue Watching</button>

如果这个按钮 html 变成一个实际的按钮,我很抱歉,我不确定如何处理。

我的预期结果是“查找并等待按钮可点击”。然后单击它。我的实际结果是什么都没有发生,并且在控制台中弹出一个错误。

raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', 端口 = 49951):最大重试次数超出了 url: /session/4100d1e939db4a44f287a50f5e9be234/element(由 NewConnectionError(': 建立新连接失败: [WinError 10061] 否 可以建立连接,因为目标机器主动拒绝 它'))

【问题讨论】:

    标签: python selenium xpath css-selectors webdriverwait


    【解决方案1】:

    对我来说,我在元素可以通信之前杀死了驱动程序实例。因此,只需确认一次,如果您的代码在 config/setup 中的某处没有发生这种情况。

    【讨论】:

      【解决方案2】:

      嗯,你试图通过 LINK_TEXT 找到它并且不是链接

      可以试试这个By.xpath("//*[text()='Continue Watching']"

      【讨论】:

        【解决方案3】:

        要在所需元素上click(),您必须为element_to_be_clickable() 诱导WebDriverWait,您可以使用以下任一解决方案:

        • 使用CSS_SELECTOR

          WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button>img[src*='tv/global/watchCamcorder']"))).click()
          
        • 使用XPATH

          WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[normalize-space() = 'Continue Watching']"))).click()
          
        • 注意:您必须添加以下导入:

          from selenium.webdriver.support.ui import WebDriverWait
          from selenium.webdriver.common.by import By
          from selenium.webdriver.support import expected_conditions as EC
          

        PS:但是,您看到的错误No connection could be made because the target machine主动拒绝了它是由于其他一些原因,您可以找到一个详细讨论MaxRetryError: HTTPConnectionPool: Max retries exceeded (Caused by ProtocolError('Connection aborted.', error(111, 'Connection refused')))

        【讨论】:

        • 非常感谢您的反馈,我会尽快尝试并告诉您是否有效。
        【解决方案4】:

        我遇到了类似的错误,但后来我意识到我正在关闭驱动程序,并且在我试图删除 cookie 之后......并且正在发送错误。

        我的建议是查看您的代码并验证您是否在某些指令中关闭了驱动程序。

        还有一个建议是在单击按钮后进入睡眠状态。

        如果可能的话,尝试对元素实现高亮在这种情况下非常有帮助。

        【讨论】:

          猜你喜欢
          • 2021-07-17
          • 2019-09-24
          • 2021-02-21
          • 2020-07-19
          • 2014-01-27
          • 1970-01-01
          • 1970-01-01
          • 2014-07-25
          • 1970-01-01
          相关资源
          最近更新 更多