Locating single elements in Selenium Python

Locators Strategies in Selenium Python are methods that are used to locate elements from the page and perform an operation on the same. Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. After one has installed selenium and checked out – Navigating links using get method, one might want to play more with Selenium Python. After opening a page using selenium such as geeksforgeeks, user might want to click some buttons automatically or fill a form automatically or any such automated task. This article revolves around Locating single elements in Selenium Python.

Locator Strategies to locate single first elements

Selenium Python follows different locating strategies for elements. One can locate a element in 8 different ways. Here is a list of locating strategies for Selenium in python –

Locators Description
find_element_by_id The first element with the id attribute value matching the location will be returned.
find_element_by_name The first element with the name attribute value matching the location will be returned.
find_element_by_xpath The first element with the xpath syntax matching the location will be returned.
find_element_by_link_text The first element with the link text value matching the location will be returned.
find_element_by_partial_link_text The first element with the partial link text value matching the location will be returned.
find_element_by_tag_name The first element with the given tag name will be returned.
find_element_by_class_name the first element with the matching class attribute name will be returned.
find_element_by_css_selector The first element with the matching CSS selector will be returned.

find_element_by_id

With this strategy, the first element with the id attribute value matching the location will be returned. If no element has a matching id attribute, a NoSuchElementException will be raised.

Syntax –

driver.find_element_by_id("id_of_element")

Example –
For instance, consider this page source:



filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
 <body>
  <form id="loginForm">
   <input name="username" type="text" />
   <input name="password" type="password" />
   <input name="continue" type="submit" value="Login" />
  </form>
 </body>
<html>

chevron_right


Now after you have created a driver, you can grab an element using –

login_form = driver.find_element_by_id('loginForm')

To check practical implementation, visit – find_element_by_id() driver method – Selenium Python

find_element_by_name

With this strategy, the first element with the name attribute value matching the location will be returned. If no element has a matching name attribute, a NoSuchElementException will be raised.

Syntax –

driver.find_element_by_name("name_of_element")

Example –
For instance, consider this page source:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
 <body>
  <form id="loginForm">
   <input name="username" type="text" />
   <input name="password" type="password" />
   <input name="continue" type="submit" value="Login" />
  </form>
 </body>
<html>

chevron_right


Now after you have created a driver, you can grab an element using –

element = driver.find_element_by_name('username')

To check practical Implementation, visit – find_element_by_name() driver method – Selenium Python

find_element_by_xpath

With this strategy, the first element with pattern of xpath matching the location will be returned. If no element has a matching element attribute, a NoSuchElementException will be raised.



Syntax –

driver.find_element_by_xpath("xpath")

Example –
For instance, consider this page source:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
 <body>
  <form id="loginForm">
   <input name="username" type="text" />
   <input name="password" type="password" />
   <input name="continue" type="submit" value="Login" />
  </form>
 </body>
<html>

chevron_right


Now after you have created a driver, you can grab an element using –

login_form = driver.find_element_by_xpath("/html/body/form[1]")
login_form = driver.find_element_by_xpath("//form[1]")

To check Practical Implementation, visit – find_element_by_xpath() driver method – Selenium Python

With this strategy, the first element with the link text value matching the location will be returned. If no element has a matching link text attribute, a NoSuchElementException will be raised.

Syntax –

driver.find_element_by_link_text("Text of Link")

Example –
For instance, consider this page source:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
 <body>
  <p>Are you sure you want to do this?</p>
  <a href="continue.html">Continue</a>
  <a href="cancel.html">Cancel</a>
</body>
<html>

chevron_right


Now after you have created a driver, you can grab an element using –

login_form = driver.find_element_by_link_text('Continue')

To check practical Implementation, visit – find_element_by_link_text() driver method – Selenium Python

With this strategy, the first element with the partial link text value matching the location will be returned. If no element has a matching partial link text attribute, a NoSuchElementException will be raised.



Syntax –

driver.find_element_by_partial_link_text("Text of Link")

Example –
For instance, consider this page source:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
 <body>
  <p>Are you sure you want to do this?</p>
  <a href="continue.html">Continue</a>
  <a href="cancel.html">Cancel</a>
</body>
<html>

chevron_right


Now after you have created a driver, you can grab an element using –

login_form = driver.find_element_by_partial_link_text('Conti')

To check practical implementation, visit – find_element_by_partial_link_text() driver method – Selenium Python

find_element_by_tag_name

With this strategy, the first element with the given tag name will be returned. If no element has a matching tag name, a NoSuchElementException will be raised.

Syntax –

driver.find_element_by_tag_name("Tag name")

Example –
For instance, consider this page source:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
 <body>
  <h1>Welcome</h1>
  <p>Site content goes here.</p>
</body>
<html>

chevron_right


Now after you have created a driver, you can grab an element using –

login_form = driver.find_element_by_tag_name('h1')

To check practical Implementation, visit – find_element_by_tag_name() driver method – Selenium Python

find_element_by_class_name

With this strategy, the first element with the matching class attribute name will be returned. If no element has a matching class attribute name, a NoSuchElementException will be raised.



Syntax –

driver.find_element_by_class_name("class_of_element")

Example –
For instance, consider this page source:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
 <body>
  <p class="content">Site content goes here.</p>
</body>
<html>

chevron_right


Now after you have created a driver, you can grab an element using –

content = driver.find_element_by_class_name('content')

To check practical Implementation, visit – find_element_by_class_name() driver method – Selenium Python

find_element_by_css_selector

With this strategy, the first element with the matching CSS selector will be returned. If no element has a matching CSS selector, a NoSuchElementException will be raised.

Syntax –

driver.find_element_by_css_selector("CSS Selectors")

Example –
For instance, consider this page source:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
 <body>
  <p class="content">Site content goes here.</p>
</body>
<html>

chevron_right


Now after you have created a driver, you can grab an element using –

content = driver.find_element_by_css_selector('p.content')

To check practical implementation, visit – find_element_by_css_selector() driver method – Selenium Python




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.