等待元素消失

13

我正在使用Python 2.7中的selenium,我有这段代码,但我正在寻找更有效的方法来完成这个任务:

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

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

您不需要使用 while 循环,因为 WebDriverWait() 函数已经等待了您设置的时间。


我想要相反的情况,即当元素不存在时。 - User
你的意思是,你想要等待元素变得不可见/不存在? - Mahsum Akbas
元素的不可见性与元素的存在有些不同,因此这并不是对这个问题的精确回答。上面是对一个不同但非常相似的关于不可见性的问题的回答。 - justnpT
函数缺少一个闭合括号“)” 。 - Biswajit Chopdar
我会在这段代码中添加try/except。如果一个元素不存在怎么办?那么就会出现StaleElementReferenceException异常。 - vitaliis

10

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.
"""

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接