使用 Selenium webdriver 2,我遇到以下问题:
我通过
本地化元素find_element_by_link_text("Archive")
这是未单击时元素的 HTML:
<a onclick="Event.GetDataEvent(226780, 'LH', 19, 19, '',
'http://www.example.com/');"
href="javascript:void(0);">Archive</a>
当点击时,它会变成(注意class="active"
):
<a class="active" onclick="Event.GetDataEvent(226780, 'LH', 19, 19, '',
'http://www.example.com/');"
href="javascript:void(0);">Archive</a>
我想等待元素变为class="active"
。我认为要走的路是使用 WebDriverWait 但我实际上如何告诉 selenium 等到
find_element_by_link_text("Archive")
(没有其他 link_text)有 class="active"
吗?
最佳答案
使用selenium.webdriver.support.wait.WebDriverWait.until(..)
:
until(method, message='')
Calls the method provided with the driver as an argument until the return value is not False.
from selenium.webdriver.support.wait import WebDriverWait
...
def archive_link_is_active(driver):
cls = driver.find_element_by_link_text("Archive").get_attribute('class')
return cls and 'active' in cls
WebDriverWait(driver, 10).until(archive_link_is_active)
关于python - 等到一个元素被激活, Selenium ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18673766/