nutship
nutship

Reputation: 4924

Wait until an element is active, selenium

Using Selenium webdriver 2, I'm faced with the following problem:

I localize an element by

find_element_by_link_text("Archive")

This is an HTML of the element when unclicked:

<a onclick="Event.GetDataEvent(226780, 'LH', 19, 19, '', 
'http://www.example.com/');" 
href="javascript:void(0);">Archive</a>

When clicked it turns into (notice class="active"):

<a class="active" onclick="Event.GetDataEvent(226780, 'LH', 19, 19, '', 
'http://www.example.com/');" 
href="javascript:void(0);">Archive</a>

I want to wait for the element to be class="active". I think the way to go is by using WebDriverWait but how do I actually tell selenium to wait until

find_element_by_link_text("Archive") (and no other link_text) has class="active" ?

Upvotes: 2

Views: 3815

Answers (1)

falsetru
falsetru

Reputation: 368954

Use 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)

Upvotes: 3

Related Questions