Explore Courses Blog Tutorials Interview Questions
+3 votes
in DevOps and Agile by (29.3k points)

I have a problem - I am using the selenium (firefox) web driver to open a webpage, click a few links, etc. then capture a screenshot.

my script runs fine from the CLI, but when running via a cronjob it is not getting past the first find_element() test. I need to add some debug, or something to help me figure out why it is failing.

Basically, I have to click a 'log in' anchor before going to the login page. The construct of the element is:

<a class="lnk" rel="nofollow" href="/login.jsp?destination=/secure/Dash.jspa">log in</a>

I am using the find_element By LINK_TEXT method:

login = driver.find_element(By.LINK_TEXT, "log in").click()

I am a bit of a Python Noob, so I am battling with the language a bit...

A) How do I check that the link is actually being picked up by python? Should I use a try/catch block?

B) Is there a better/more reliable way to locate the DOM element than by LINK_TEXT? E.g. In JQuery, you can use a more specific selector $('a.lnk:contains(log in)').do_something();

I have solved the main problem and it was just finger trouble - I was calling the script with incorrect parameters - Simple mistake.

I'd still like some pointers on how to check whether an element exists. Also, an example/explanation of implicit/explicit Waits instead of using a crappy time.sleep() call.

5 Answers

+5 votes
by (50.2k points)

The easiest way to check an element exists is to simply call

Find_element in a try/catch block.

Two reasons that I find elements without using their text

1. The text is more likely to change

2. Sometimes I won’t be able to run tests against localized builds.

To resolve this 

You can use the Xpath to find a parent or its ancestor which has an ID through which you can find its child that matches.


you can request id for the link itself.

Go through these docs you will find good examples for try/catch blocks.

Learn Selenium with the help of this Selenium Tutorial by Intellipaat.

To learn in-depth about Selenium, sign up for an industry based Selenium training.

If you are interested to learn Python from Industry experts, you can sign up for this Python Certification Course by Intellipaat.

Checking if element exists with Python Selenium
by (29.3k points)
Thanks for the info. It helps for me.
by (19.9k points)
Thank you. You solution worked for me.
by (19k points)
Your answer solved my problem.
by (33.1k points)
This answer was helpful. Thanks.
+2 votes
by (108k points)

For your first question, you can perform the following code:

from selenium.common.exceptions import NoSuchElementException        

def check_exists_by_xpath(xpath):



    except NoSuchElementException:

        return False

    return True

And for your second question, you can take the XPath as a standard throughout all your scripts and create functions as above mentions for universal use.

Checking if element exists with Python Selenium
by (19.7k points)
Yea, got it, thanks!
by (32.1k points)
Faced the same problem a few weeks ago. Thanks! You helped me solve it.
+2 votes
by (44.4k points)

So, doing this will give you the list of elements instead of just the element, and you can count it if it is zero then it doesn’t exist. You can do something like this:

if driver.find_elements_by_css_selector('#element'):

    print "Element exists"

Checking if element exists with Python Selenium
by (29.5k points)
hi, thanks this worked for me , well explained solution!!!
+1 vote
by (47.2k points)

You can also try a solution without try&catch and without new imports:

if len(driver.find_elements_by_id('blah')) > 0: 


0 votes
by (106k points)

You can do it more concisely by using the below mentioned piece of code:- 

driver.find_element_by_id("some_id").size() != 0

Browse Categories