等待Python中的元素存在

时间:2017-06-23 01:05:42

标签: python selenium timeout

我有一个正在运行的代码,但有时我会在30秒内达到超时,有时会在80或更多(取决于服务器端),所以关键是 - 如何在我可以查看元素后停止代码而不是等到120秒过去了? 好吧,我想要实现的是在屏幕上没有元素后停止代码:

 def isElementPresent(self):
        try:
            wait = WebDriverWait(self.driver, 120)
            wait.until(EC.invisibility_of_element_located((By.NAME, 'GETTING NEW IMAGE FROM HOME SYSTEM')))
        except TimeoutException:
            print('Camera Timeout')

2 个答案:

答案 0 :(得分:1)

您需要删除while循环。只需使用WebDriverWait

即可
def isElementPresent(self):
    wait = WebDriverWait(self.driver, 120)
    try:
        wait.until(EC.presence_of_element_located((By.NAME, 'GETTING NEW IMAGE FROM HOME SYSTEM')))
    except TimeoutException:
        print('The element appears')
    try:
        wait.until(EC.invisibility_of_element_located((By.NAME, 'GETTING NEW IMAGE FROM HOME SYSTEM')))
    except TimeoutException:
        print('The element does not disappear')

答案 1 :(得分:0)

documentation开始,尝试 Cn.CommandTimeout = 50 Cn.ConnectionTimeout = 50 条件:

visibility_of_element_located