1

How can you use driver.refresh() if in the wait time WebDriverWait(driver, 30) cant find an element, it refreshes the page and then retry to find the element?

This is the element Im looking for

quote = wait.until(EC.element_to_be_clickable((By.XPATH, '//h4[@class="quote-number ng-binding"]'))).text.split("#")[1]

Thanks

1
  • Welcome. There are a lot of questions on Stackoverflow on this topic. Have you looked at them, if none don't apply, then how is your problem different to them? Please re-read How do I ask a good question, particularly the section on "Search and Research", and the edit your question to show your research. Commented Jan 25, 2019 at 1:05

2 Answers 2

6

If the condition is not fulfilled WebdriverWait throws an exception- TimeoutException; you can catch it, and retry.
In the same time, you want to limit the number of retries - the element may not ever appear, you don't want this block to run forever.

retries = 1
while retries <= 5:
    try:
        quote = wait.until(EC.element_to_be_clickable((By.XPATH, '//h4[@class="quote-number ng-binding"]'))).text.split("#")[1]
        break
    except TimeoutException:
        driver.refresh()
        retries += 1
Sign up to request clarification or add additional context in comments.

2 Comments

I think you should add break after wait in try block, otherwise it will execute infinite time, if it do not timeout.
Actually, you're quite right @WaqasAli! Thanks, updated.
-1

As per my understanding, you want to find an element in some page and if you couldn't find it then you need to refresh the page to retry finding it again. If this is your requirement, then you can do like this :

wait = WebDriverWait(driver, 30);
try:
    quote = wait.until(EC.element_to_be_clickable((By.XPATH, '//h4[@class="quote-number ng-binding"]'))).text.split("#")[1]
except TimeoutException:
    driver.refresh()

In the above code, the try block will throw 'TimeoutException' if the element is not found within the given time out. The except block will catch that exception and it matches then it will refresh the page.

The above code will do this activity only once. If you want to continue this process until you find an element then use the below code:

notFound = True
while notFound:
    wait = WebDriverWait(driver, 30);
    try:
        quote = wait.until(EC.element_to_be_clickable((By.XPATH, '//h4[@class="quote-number ng-binding"]'))).text.split("#")[1]
        notFound = False
    except TimeoutException:
        driver.refresh()

But the above solution is not recommended because if it doesn't find an element that it is looking for then the code will go to an infinity looping state. To avoid this, I recommend you use the FluentWait like below :

wait = WebDriverWait(driver, 60, poll_frequency=5, ignored_exceptions=[NoSuchElementException, StaleElementReferenceException]);
quote = wait.until(EC.element_to_be_clickable((By.XPATH, '//h4[@class="quote-number ng-binding"]'))).text.split("#")[1]

Which will search an element every 5 seconds by ignoring NoSuchElementException, StaleElementReferenceException exceptions upto 1 minute of time. I hope it helps...

6 Comments

the exception NoSuchElementException is handled by WebDriverWait itself, so it won't ever be raised. And your last suggestion is actually to extend the waited for interval to 60 seconds, with a pooling one of 5 seconds - not a bad idea by itself, especially for UI ops slower to react, but - it will not reload the page, thus working with the same DOM.
@Todor Minakov Yes, it will not refresh the page and will poll every 5 seconds to find an element again. Thanks for catching 'NoSuchElementException' is handled by WebDriverWait itsefl, missed this point.
I am bringing up the not-refresh, as OP already is using WebdriverWait, while the question itself is "How to reload page selenium if WebDriverWait timeout?". Your proposal doesn't add a lot, except for wait for the element some more time.
I'm recommending him to avoid refreshing the page. He is refreshing the page because he couldn't find an element in the specified WebdriverWait time and he is thinking that, he may find an element if he do the refreshing.
I have tried answering the question first then I have tried recommending some other way around. Thanks for the advice or suggestion or whatever it is! As I'm new to this platform, may be I need some time to learn all this stuff.
|

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.