Selenium WebDriver 中是否有一种现有的方法来等待元素获得或失去一个类?例如,我选择的 HTML 如下所示:
<select id="select-1" class="selectBox ui-state-error" row-index="0" tabindex="-1" aria-hidden="true">
...
</select>
有问题的类是ui-state-error
。这基本上在我的下拉列表中显示红色突出显示。基本上,我想设置一个显式等待,直到我的 select 元素获得类 ui-state-error
或丢失类 ui-state-error
。
我不确定这是否可能。
Python版本:3.4.3。 Selenium 的 Python 绑定(bind):2.46.0最佳答案
没有内置方法可以实现它。您需要写一个 custom Expected Condition :
from selenium.webdriver.support import expected_conditions as EC
class wait_for_class(object):
def __init__(self, locator, class_name):
self.locator = locator
self.class_name = class_name
def __call__(self, driver):
try:
element_class = EC._find_element(driver, self.locator).get_attribute('class')
return element_class and self.class_name in element_class
except StaleElementReferenceException:
return False
用法:
wait = WebDriverWait(driver, 10)
wait.until(wait_for_class((By.ID, 'select-1'), "ui-state-error"))
预期的条件基本上是可调用的,这意味着您可以只编写一个函数,但我喜欢遵循它们在 python-selenium 内部作为类实现的方式。
关于python - 我怎样才能等到一个元素获得或失去一个类别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31076841/