1

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')
5
  • What is 'GETTING NEW IMAGE FROM HOME SYSTEM'? IS the value changed every loop? I'm just curious why you need that loop. Commented Jun 23, 2017 at 1:25
  • no, this name is same all the time. just appears and disappears as the camera changes image Commented Jun 23, 2017 at 1:26
  • I can't believe that sometimes you get timeout in 30 or 80 sec. When it enters the loop, it will wait for the element for 120 sec. If it found the element, it will break the while loop immediately. Otherwise, it will print 'Camera Timeout' and then go to next loop, and wait for the element for 120 sec. again. The point is the timeout error is always at 120 sec. But the time to break the loop depends on how long does it take to find the element. Commented Jun 23, 2017 at 1:34
  • that's correct, I don't want to wait for 120 secs all the time, because it might take less, but the program still waits. Commented Jun 23, 2017 at 1:37
  • Where do you call isElementPresent? Can you show more code? Commented Jun 23, 2017 at 2:22

2 Answers 2

1

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')
Sign up to request clarification or add additional context in comments.

3 Comments

Tried this, still waits for 120 secs, however the element has gone before that time
I'm sorry. Do you wait until the element disappears?
Sorry for misunderstanding. I wait until I see the element. then I wait until it is gone, yes
0

From the documentation, try the visibility_of_element_located condition:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.