Search code examples
pythonseleniumtimeout

Wait for element present in Python


I got a running code, but sometimes I get timeout in 30 seconds, sometimes in 80 or more (depends on server side), so the point is - how to make my code stop once I can view the element and not to wait until 120 seconds passed? Well, what I am trying to achieve is stop the code once the element is NOT PRESENTED on the screen:

 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')

Solution

  • You need to remove the while loop. Just use the 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')