User
User

Reputation: 24731

Wait until element is not present

I'm using selenium in Python 2.7 and I have this code, but I'm looking for a more efficient way to do this:

while True:
    try:
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, 'button'))
        )   
    except:
        break

Upvotes: 13

Views: 18028

Answers (2)

Mahsum Akbas
Mahsum Akbas

Reputation: 1583

 element = WebDriverWait(driver, 10).until(
            EC.invisibility_of_element_located((By.ID, 'button')))

you don't need to use while. it already waits for time that you present in WebDriverWait() function.

Upvotes: 21

dmr
dmr

Reputation: 556

1) Use staleness_of from expected condition

class staleness_of(object):
""" Wait until an element is no longer attached to the DOM.
element is the element to wait for.
returns False if the element is still attached to the DOM, true otherwise.
"""

2) WebDriverWait(driver, 10).until_not(...)

Upvotes: 11

Related Questions