jac*_*118 4 python selenium python-3.x selenium-webdriver
我有一个属性为“ aria-busy”的元素,当搜索和完成数据时,该元素从true变为false。如果达到20秒并且属性未从true更改为false,则如何使用硒预期条件和显式等待来等待默认时间(例如20秒)。抛出异常。我有以下内容,但它实际上没有用
import selenium.webdriver.support.ui as ui
from selenium.webdriver.support import expected_conditions as EC
<div id="xxx" role="combobox" aria-busy="false" /div>
class Ele:
def __init__(self, driver, locator)
self.wait = ui.WebDriverWait(driver, timeout=20)
def waitEle(self):
try:
e = self.driver.find_element_by_xpath('//div[@id='xxxx']')
self.wait.until(EC.element_selection_state_to_be((e.get_attribute('aria-busy'), 'true')))
expect:
raise Exception('wait timeout')
Run Code Online (Sandbox Code Playgroud)
预期条件只是可调用的,您可以将其定义为简单函数:
def not_busy(driver):
try:
element = driver.find_element_by_id("xxx")
except NoSuchElementException:
return False
return element.get_attribute("aria-busy") == "false"
self.wait.until(not_busy)
Run Code Online (Sandbox Code Playgroud)
但是,更通用和模块化的一点是,遵循内置的Expected Conditions的样式,并使用覆盖的__call__()magic方法创建一个类:
from selenium.webdriver.support import expected_conditions as EC
class wait_for_the_attribute_value(object):
def __init__(self, locator, attribute, value):
self.locator = locator
self.attribute = attribute
self.value = value
def __call__(self, driver):
try:
element_attribute = EC._find_element(driver, self.locator).get_attribute(self.attribute)
return element_attribute == self.value
except StaleElementReferenceException:
return False
Run Code Online (Sandbox Code Playgroud)
用法:
self.wait.until(wait_for_the_attribute_value((By.ID, "xxx"), "aria-busy", "false"))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3328 次 |
| 最近记录: |