Checking If Element Exists With Python Selenium
Answer : a) from selenium.common.exceptions import NoSuchElementException def check_exists_by_xpath(xpath): try: webdriver.find_element_by_xpath(xpath) except NoSuchElementException: return False return True b) use xpath - the most reliable. Moreover you can take the xpath as a standard throughout all your scripts and create functions as above mentions for universal use. UPDATE : I wrote the initial answer over 4 years ago and at the time I thought xpath would be the best option. Now I recommend to use css selectors . I still recommend not to mix/use "by id", "by name" and etc and use one single approach instead. None of the solutions provided seemed at all easiest to me, so I'd like to add my own way. Basically, you get the list of the elements instead of just the element and then count the results; if it's zero, then it doesn't exist. Example: if driver.find_elements_by_css_selector('#element')...