I found this answer about checking the visibility of an element. My problem with this answer is, that it never returns "Element not found". Not only that! It take a very long time to give the error message (below).
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get('http://www.google.com')
element = driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/a[2]') #this element exists
if element.is_displayed():
print("Element found")
else:
print("Element not found")
hidden_element = driver.find_element(By.XPATH,'/html/body/div[1]/div[1]/a[20]') #this one doesn't exist
if hidden_element.is_displayed():
print("Element found")
else:
print("Element not found")
I need something that is more efficient and returns False or something another than this error message:
RemoteError@chrome://remote/content/shared/RemoteError.jsm:12:1 WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:192:5 NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.jsm:404:5 element.find/</<@chrome://remote/content/marionette/element.js:291:16