Selenium How to Continue when Not Found

Question:

I have this code:

import selenium.webdriver as webdriver
import time
import requests
from selenium.webdriver.chrome.options import Options
from usp.tree import sitemap_tree_for_homepage
import os
from selenium.webdriver.common.by import By
from urllib.parse import urlparse
from urllib.parse import parse_qs

options = Options()
options.add_argument('--allow-running-insecure-content')
options.add_argument('--ignore-certificate-errors')

driver = webdriver.Chrome()

sitemap = 'https://adesivimoto.eu/sitemap.xml'
next_pages = []

tree = sitemap_tree_for_homepage(sitemap)
for page in tree.all_pages():
    driver.get(page.url)
    print("visito "+page.url)
    time.sleep(2)
    
    if driver.find_element(By.CLASS_NAME, "next"):
        next_page = driver.find_element(By.CLASS_NAME, "next").get_attribute('href')
        next_pages.append(next_page)

for next_page in next_pages:
    print(next_page)
    
os.system("rundll32.exe powrprof.dll,SetSuspendState 0,1,0")

Where the code says next_page = driver.find_element(By.CLASS_NAME, "next"), if the element is not available, there will be an error. I tried to work around this using if, but was not successful.
How can I make is so that, if the element is not available, the code just goes to the next iteration of the loop?

Asked By: Luigi

||

Answers:

try: # try getting the element
    next_page = driver.find_element(By.CLASS_NAME, "next")
    # code, if element gets found
    # ....
except:
    pass # not found ==> do nothing

If that’s not what you need, feel free to leave a comment and I’ll edit my answer.

Answered By: kaliiiiiiiii

Wrap up the line of code with a try-except{} block inducing WebDriverWait for the visibility_of_element_located() and catching TimeoutException as follows:

try:
    next_page =  WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "next"))).get_attribute('href')
    next_pages.append(next_page)
except TimeoutException:
    pass
Answered By: undetected Selenium