How To Catch Network Failures While Invoking Get() Method Through Selenium And Python?
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: If I reload the page 99% it will load f
Solution 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-definedpartialURL
from the url. - You can handle this code within a
try{}
block with expected_conditions method title_contains() and in case ofTimeoutException
invokebrowser.get(link)
again within thecatch{}
block. Your modified code block will be:
#importsfrom 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 inrange(3): try: WebDriverWait(browser, 10).until(EC.title_contains(partialTitle)) breakexcept TimeoutException: browser.get(link) logger.warning('there is no valid connection')
Post a Comment for "How To Catch Network Failures While Invoking Get() Method Through Selenium And Python?"