等待 Python 中存在的元素



我得到了一个正在运行的代码,但有时我在 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')

您需要删除 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')

从文档中,尝试visibility_of_element_located条件:

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

相关内容

  • 没有找到相关文章

最新更新