Karenmtz
Karenmtz

Reputation: 29

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

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

Upvotes: 1

Views: 62

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193188

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

Upvotes: 1

Related Questions