0

I am using Chrome with selenium and the test run well, until suddenly internet/proxy connection is down, then browser.get(url) get me this: enter image description here

If I reload the page 99% it will load fine, what is the proper way to handle this ?

MY CODE:

def web_adress_navigator(browser, link):
"""Checks and compares current URL of web page and the URL to be navigated and if it is different, it does navigate"""

try:
    current_url = browser.current_url
except WebDriverException:
    try:
        current_url = browser.execute_script("return window.location.href")
    except WebDriverException:
        current_url = None

if current_url is None or current_url != link:
    retries = 5
    while retries > 0:
        try:
            browser.get(link)
            break
        except TimeoutException:
            logger.warning('TimeoutException when tring to reach page')
            retries -= 1
            while not is_connected():
                sleep(60)
                logger.warning('there is no valid connection')

I am not getting into TIMEOUT EXCEPTION but to the break part.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Sion C
  • 439
  • 2
  • 14

1 Answers1

1

As per your question and your code trials as you are trying to access the url passed through the argument link you can adapt a strategy where:

  • Your program will make pre-defined number of trials to invoke the desired url, which you can pass through range().
  • Once you invoke get(link) your program will invoke WebDriverWait for a predefined interval for the url to contain a pre-defined partialURL from the url.
  • You can handle this code within a try{} block with expected_conditions method title_contains() and in case of TimeoutException invoke browser.get(link) again within the catch{} block.
  • Your modified code block will be:

    #imports
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import TimeoutException
    # other code works
    browser.get(link)
    for i in range(3):
        try:
            WebDriverWait(browser, 10).until(EC.title_contains(partialTitle))
            break
        except TimeoutException:
            browser.get(link)
    logger.warning('there is no valid connection')
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    I don't get you, the url for the fail example is the url in "link" var, so in this case it will not refresh! or am I missing something ? – Sion C Aug 23 '18 at 14:27
  • Maybe it will be best to wait on some part of the title? EC.title_contains('Amazon') – Sion C Aug 23 '18 at 14:32
  • As you are having the _argument_ **link**, you can consider to set _partialLinkText_ to **link** (but preferably partial link). If the _partialLinkText_ is not found once the navigation completes (due to network problems), `catch{}` will retry the _url_. Let me know if that answers your question. Ofcoarse you can validate as per _Page Title_ as well. – undetected Selenium Aug 23 '18 at 14:35
  • I am really sorry I don't get what partialLinkText is. I tried to find out in google but came but confused. – Sion C Aug 23 '18 at 14:44
  • My bad, I have used the wrong nomenclature. I have changed the verbatim from `partialLinkText` to **partialURL** . For example if the complete _url_ is `https://docs.seleniumhq.org/download/`, **partialURL**s are **`docs`**, **`seleniumhq`**, **`org`** and **`download`**. Let know if this answers your question. – undetected Selenium Aug 23 '18 at 14:53
  • I don't see how that help, since going to www.google.com will bring me title "google" that will detected as false. – Sion C Aug 23 '18 at 15:07
  • I did take you solution but chcking the title is not an url by it self: def find_url(string): from urllib.parse import urlparse o = urlparse(string) if hasattr(o, 'netloc') and hasattr(o, 'scheme') and o.netloc != '': return True return False – Sion C Aug 23 '18 at 15:08
  • Check the `expected_conditions` method [**`url_contains(url)`**](https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#selenium.webdriver.support.expected_conditions.url_contains) – undetected Selenium Aug 23 '18 at 15:11
  • 1
    @SionC I did a mini research over your usecase and as you mentioned earlier in your comment `ExpectedCondition` method **`titleContains()`** seems to be best fit. I have updated my answer accordingly. Let me know your thoughts about it. – undetected Selenium Aug 24 '18 at 15:40