Locating elements in selenium python

To interact with any web page using selenium, you need to first find webelement/s to perform any action on element.

All the actions you do on a webpage like entering a text into a textbox or textarea and after filling out the form, you may want to click the button to submit data. There are many other operations we do like mousehover, selecting a value from dropdown and navigating between multiple windows/frames which we will discuss more in detail in coming tutorials.

In Python, Selenium WebDriver API supports multiple ways to identify element/s by using below locator strategies. For Java please refer Selenium locators in java

1. By ID:-

Syntax:- driver.find_element_by_id()

Example:-
eleUserMessage = driver.find_element_by_id("user-message")
eleUserMessage.clear()
eleUserMessage.send_keys("Test Python")

2. By NAME:-

Syntax:- driver.find_element_by_name()

Example:-
elemFormControl=driver.find_element_by_name("comment")
elemFormControl.clear()
elemFormControl.send_keys("this is test comment")

3. By XPath:-

Syntax:- driver.find_element_by_xpath()

Example:-
elemcheckBtn=driver.find_element_by_xpath("//input[@class='cb1-element']")
elemcheckBtn.click()

You can for more examples here Xpath and CSS Examples

4. By CLASS:-

Syntax:- driver.find_element_by_class_name()

Example:-
elemSendBtn=driver.find_element_by_class_name("btn")
elemSendBtn.click()

5. By TAGNAME:-

Syntax: driver.find_element_by_tag_name()

Example:-
eleALink = driver.find_element_by_tag_name("a") // return the first anchor element
eleALink.click()

Example 2:-
eleTable = driver.find_element_by_id("mainTable")
#to get the count of number of rows
List totalRows = eleTable.find_element_by_tag_name("tr")
print(rows.size());

6. By CSS Selector:-

Syntax:- driver.find_element_by_css_selector()

Example:-
testButton = driver.find_element_by_class_name("test_button_unknown")
testButton.click()

Check for more examples on CSS Selectors

7. By Link Text:-

Syntax:- driver.find_element_by_link_text()

Example:-
eleHome = browser.find_element_by_link_text("Home")
eleHome.click()

8. By Partial Link text:

Syntax:- driver.find_element_by_partial_link_text()

Example:-
eleContactUsLink = driver.find_element_by_partial_link_text("Contact")
eleContactUsLink.click()

To find multiple elements, you have to use find_elements_*, these methods will return a list of webelements matching with the specified locator.

Syntax:-

find_element_*

- will return first web element.

find_elements_*

- will return you list of web elements

Example:
all_divs = driver.find_elements_by_xpath("//div[@class='section']")
for div in all_divs:
print div.text

In the above example, find_elements_by_xpath() returns all matching elements with div class named 'section', which is a list, you can loop through the list and get text attribute for each of the element.

If there are no matching elements with the specified locator, it just returns an empty list. But when we use find_element_*, it throws a NoSuchElementException exception.

Selenium Tutorials: 

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.