等到元素不存在

Use*_*ser 8 python selenium

我在Python 2.7中使用selenium并且我有这个代码,但我正在寻找一种更有效的方法来做到这一点:

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

Mah*_*bas 12

 element = WebDriverWait(driver, 10).until(
            EC.invisibility_of_element_located((By.ID, 'button'))
Run Code Online (Sandbox Code Playgroud)

你不需要使用while.它已经等待你在WebDriverWait()函数中出现的时间了.

  • 好的,在我的回答中,是等待元素的“隐身”。 (2认同)

dmr*_*dmr 8

1)使用预期条件下的staleness_of

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.
"""
Run Code Online (Sandbox Code Playgroud)

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