首页 > 解决方案 > 如何在 Python 中使用 Selenium 设置动态显式等待?

问题描述

我几天前构建的一个程序在这里遇到了一个小问题,所以我将非常简单地解释它:

我正在使用我的程序从页面中抓取数据,为此,我设置了这个显式等待:

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH,'/this_is_the_XPATH')))

在大多数情况下,它工作得很好,但是,有时上面的显式等待会导致此错误:

超时异常

这使我的程序停止并不得不再次重复一个漫长的过程。

我意识到设置为10秒的等待参数并不总是一个好的参数,所以我想知道你们是否有办法将此参数设置为一个变量,该变量总是获取页面的确切加载时间(以秒为单位)已完全加载。

其他更简单的想法可能是强制重复该过程,直到最终加载元素的可见性,例如使用尝试和异常块,但我不知道我应该在异常块中键入什么来一遍又一遍地重复尝试直到完成。

与往常一样,感谢您的反馈,感谢您提前阅读。

标签: pythonseleniumweb-scrapingwebdriverwaitexpected-condition

解决方案


You are using the Expected Conditions function from Selenium. That initial wait of 10 seconds is simply setting the maximum time that it is allowed to wait, should the Expected Conditions function fail to get anything.

You may want to raise the 10 second max wait, if it keeps failing.

The wait will end early (ie. Not 10 seconds) if the Expected Conditions condition is met.

Thus the important part is to ensure the EC function is working as intended. I like to use element_to_be_clickable as opposed to your visibility_of_element_located, perhaps give that a try, (or others, full list of conditions here)

Try this:

from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 20) #set a max wait time
element = wait.until(EC.element_to_be_clickable((By.XPATH, 'some xpath'))) #set the dynamic wait, which should end the wait early (ie not 20 seconds)

推荐阅读