Home > Enterprise >  How can I make the script continue to open the links in web pages with Python and Selenium?
How can I make the script continue to open the links in web pages with Python and Selenium?

Time:12-19

I have a script that reads the links that are in excel file, it opens each link fine, but when it encounters an error in automatic it marks error and closes the script. How can I make the script continue to open the links without stopping on the page that has an error?

I accept any suggestion or improvement for script in general. It's just a part of my code.

for url in mylist:
    driver.get(url) 
    driver.maximize_window()
    driver.implicitly_wait(val) 
    timestamp = datetime.datetime.now().strftime('%d_%m_%Y')
    driver.save_screenshot(str(shark) "/" str(cont) '_' timestamp '.png') 
    cont  = 1

CodePudding user response:

Wrap up your code block in a try-except{} block as follows:

for url in mylist:
    try:
        driver.get(url) 
        driver.maximize_window()
        driver.implicitly_wait(val) 
        timestamp = datetime.datetime.now().strftime('%d_%m_%Y')
        driver.save_screenshot(str(shark) "/" str(cont) '_' timestamp '.png') 
        cont  = 1
    except:
        continue
  • Related