【问题标题】:Python Selenium TimeoutException error even with wait until clickable and except TimeoutException:Python Selenium TimeoutException 错误,即使等待直到可点击并且 TimeoutException 除外:
【发布时间】:2022-01-08 17:51:57
【问题描述】:

我的 selenium webdriver 由于

而不断崩溃

TimeoutException:消息:超时:接收消息超时 渲染器:298.972

cookie 弹出窗口打开,但脚本没有点击它, 在像 20 driver.get(url) 一样,19 次会接受 cookie,但第 20 次将无法接受 cookie,虽然窗口已经打开,我尝试使用下面的代码但仍然失败。

retries = 1
while retries <= 5:
    try:
        element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@class="coi-banner__accept"]'))) #wait until cookies clickable
        element.click()
        break
    except TimeoutException:
        driver.refresh()            
        element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@class="coi-banner__accept"]'))) #wait until cookies clickable
        element.click()
        retries += 1

【问题讨论】:

  • 你能分享一个指向那个页面的链接吗?
  • @Prophet novasol.com

标签: python selenium selenium-webdriver webdriver timeoutexception


【解决方案1】:

尝试使用 driver.execute_script() 而不是 element.click()

htmlElement = driver.find_element_by_xpath('//*[@class="coi-banner__accept"]')
driver.execute_script("arguments[0].click();", htmlElement)

【讨论】:

    【解决方案2】:

    一般来说

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@class="coi-banner__accept"]'))) #wait until cookies clickable
    element.click()
    

    应该可以。从您共享的代码中,我看不出为什么它在 95% 的情况下有效,但在 5% 的情况下失败。
    我所看到的:
    您正试图在导致TimeoutExceptionexcept 块中找到完全相同的条件。
    因此,如果 Selenium 以某种方式无法找到与此定位器 //*[@class="coi-banner__accept"] 匹配的元素可点击并抛出 TimeoutException 等待 except 中的相同条件将给您相同的结果.... 此外,我认为将其置于 5 次尝试的循环中是没有意义的。
    如果找到元素,单击并关闭 - 没有意义再试一次。
    如果您在第一次尝试中无法做到这一点 - 这将在第一次尝试时抛出 TimeoutException 异常,您将永远不会在这里继续第二次尝试......

    【讨论】:

    • 由于某种原因有时它无法识别弹出的cookies,我想添加例外:刷新浏览器/链接并尝试再次查找元素。
    • 好的,刷新它应该可以工作。
    【解决方案3】:

    我运行了下面的脚本超过 20 次,但每次都能点击所需的按钮。

    我所要做的基本上就是将定位器从 XPath 更改为 CSS:

    driver = webdriver.Chrome(driver_path)
    driver.maximize_window()
    wait = WebDriverWait(driver, 30)
    
    driver.get("https://www.novasol.com/")
    
    try:
        wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick='CookieInformation.submitAllCategories();']"))).click()
        print('Clicked it')
    except:
        print('Either element was not found, or Bot could not click on it.')
        pass
    

    进口:

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

    更新:

    driver.get("https://www.novasol.com/")
    
    def retry_click(number_of_retries, wait_before_performing_click):
        while number_of_retries > 0:
            time.sleep(wait_before_performing_click)
            try:
                wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick='CookieInformation.submitAllCategories();']"))).click()
                break
            except:
                pass
            number_of_retries = number_of_retries - 1
    
    
    try:
        wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick='CookieInformation.submitAllCategories();']"))).click()
        print('Clicked it')
    except:
        print('Either element was not found, or Bot could not click on it.')
        driver.refresh()
        retry_click(20, 10)
        pass
    

    【讨论】:

    • 使用了这个,但这次它在第 17 次尝试时无法接受 cookie:TimeoutException:消息:超时:从渲染器接收消息超时:298.955,除了刷新链接,我该如何添加并尝试再次找到该元素?
    • @Skittles:更新,见上文。如果这仍然显示问题,我们将不得不放置一个 max_retry 变量并继续重试直到我们成功。
    • 谢谢,尝试了更新的代码,但在第 5 次尝试 get(URL) 时再次失败,我可以尝试再次运行它,因为它不会每次都失败,但想解决它它每次都有效。
    • 需要创建一个函数,在指定时间后会继续重试,只有在可以点击时才退出。
    • 你能帮我看看怎么写吗?我尝试写入 except : driver.quit( ), driver.get(link), wait.until.. 但什么也没做。
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    相关资源
    最近更新 更多