I have a Selenium script, where I enter a series of websites, and get some data. Sometimes the data does not exist, and I simply want to write something like "Cant find x data" onto my string. The current script does this:
#Getting "Status"
try:
strValue = strValue + ',' + browser.find_element_by_css_selector('#visKTTabset > div.h-tab-content > div.h-tab-content-inner > div:nth-child(12) > table > tbody > tr.stripes-even > td:nth-child(3) > span').text
except webdriver.NoSuchElementException:
StrValue = strValue + ',' + "Status not found"
The script works when the "Status" actually exists, but id does not get into the "except" part. The top of my script has this:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
browser = webdriver.Chrome(executable_path=r'C:\Selenium\chromedriver.exe')
browser.get('https://motorregister.skat.dk/dmr-front/dmr.portal?_nfpb=true&_nfpb=true&_pageLabel=vis_koeretoej_side&_nfls=false')
browser.implicitly_wait(10) # Will set the global wait to 10 seconds.gnash
I tried the solution here: Try except in python/selenium still throwing NoSuchElementException error but it didnt work.