I am trying to capture a text from an element which might have two xpaths, but I can’t make it happen.
I have tried all of the below with no luck and I’m getting this error when running my script once either of the xpath is not found.
Exception has occurred: NoSuchElementException Message: no such element: Unable to locate element:
The target element’s xpath can be either the following and the only difference is the middle div id (7 or 8).
/div/div[2]/div[1]/div[7]/div[2]/span/div/span
/div/div[2]/div[1]/div[8]/div[2]/span/div/span
if driver.find_element("xpath",'//*[@id="system"]/div/div[2]/div[1]/div[7]/div[2]/span/div/span'):
antivirus = driver.find_element("xpath",'//*[@id="system"]/div/div[2]/div[1]/div[7]/div[2]/span/div/span').text
else:
antivirus = driver.find_element("xpath",'//*[@id="system"]/div/div[2]/div[1]/div[8]/div[2]/span/div/span').text
try:
antivirus = driver.find_element("xpath",'//*[@id="system"]/div/div[2]/div[1]/div[7]/div[2]/span/div/span').text
except:
antivirus = driver.find_element("xpath",'//*[@id="system"]/div/div[2]/div[1]/div[8]/div[2]/span/div/span').text
if len(driver.find_element("xpath",'//*[@id="system"]/div/div[2]/div[1]/div[7]/div[2]/span/div/span')) > 0:
antivirus = driver.find_element("xpath",'//*[@id="system"]/div/div[2]/div[1]/div[7]/div[2]/span/div/span').text
else:
antivirus = driver.find_element("xpath",'//*[@id="system"]/div/div[2]/div[1]/div[8]/div[2]/span/div/span').text
Arash is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.