python - 等到一个元素被激活, Selenium

标签 python selenium-webdriver

使用 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/

相关文章:

python - 如何在 Python ast 中获取 "end-of-statement"的 lineno

python - 在 Selenium Webdriver 中呈现页面后禁用 Javascript

python - BeautifulSoup find_all 并不总是工作

java - WebDriver:检查元素是否存在?

java - Selenium Chrome webdriver 在登录时卡住了

javascript - 网络驱动程序错误: Cannot find elements when the xpath expression is null in javascript

调用函数时python 'module'对象不可调用

如果对象不是无,则 Python 获取属性

Python 遍历列表

python - 我如何将 xml 格式的数据从 flex 传递到 python