Locating multiple elements in Selenium Python

20 Apr, 2020
Locators Strategies in Selenium Python are methods that are used to locate single or multiple elements from the page and perform operations 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 page using selenium such as geeksforgeeks, one might want to click some buttons automatically or fill a form automatically or any such automated task. This article revolves around Locating multiple elements in Selenium Python.
Selenium Python follows different locating strategies for elements. One can locate multiple elements in 7 different ways. Here is a list of locating strategies for Selenium in python –
With this strategy, all elements with the name attribute value matching the location will be returned. If no element has a matching name attribute, a NoSuchElementException will be raised.
driver.find_elements_by_name("name_of_element")

Example –For instance, consider this page source:
<html> <body>  <form id="loginForm">   <input name="username" type="text" />   <input name="username" type="username" />   <input name="continue" type="submit" value="Login" />  </form> </body><html>
Now after you have created a driver, you can grab elements using –
elements = driver.find_elements_by_name('username')

To check practical Implementation, visit – find_elements_by_name() driver method – Selenium Python
With this strategy, all elements with pattern of xpath matching the location will be returned. If no element has a matching element attribute, a NoSuchElementException will be raised.
driver.find_elements_by_xpath("xpath")

Example –For instance, consider this page source:
<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>
Now after you have created a driver, you can grab elements using –
login_form = driver.find_elements_by_xpath("/html/body/form[1]")
login_form = driver.find_elements_by_xpath("//form[1]")

To check Practical Implementation, visit – find_elements_by_xpath() driver method – Selenium Python
With this strategy, all elements 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.
driver.find_elements_by_link_text("Text of Link")

Example –For instance, consider this page source:
<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>
Now after you have created a driver, you can grab elements using –
login_form = driver.find_elements_by_link_text('Continue')

To check practical Implementation, visit – find_elements_by_link_text() driver method – Selenium Python
With this strategy, all elements 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.
driver.find_elements_by_partial_link_text("Text of Link")

Example –For instance, consider this page source:
<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>
Now after you have created a driver, you can grab all elements using –
login_form = driver.find_elements_by_partial_link_text('Conti')

To check practical implementation, visit – find_elements_by_partial_link_text() driver method – Selenium Python
With this strategy, all elements with the given tag name will be returned. If no element has a matching tag name, a NoSuchElementException will be raised.
driver.find_elements_by_tag_name("Tag name")

Example –For instance, consider this page source:
<html> <body>  <h1>Welcome</h1>  <p>Site content goes here.</p></body><html>
Now after you have created a driver, you can grab all elements using –
login_form = driver.find_elements_by_tag_name('h1')

To check practical Implementation, visit – find_elements_by_tag_name() driver method – Selenium Python
With this strategy, the first elements with the matching class attribute name will be returned. If no element has a matching class attribute name, a NoSuchElementException will be raised.
driver.find_elements_by_class_name("class_of_element")

Example –For instance, consider this page source:
<html> <body>  <p class="content">Site content goes here.</p></body><html>
Now after you have created a driver, you can grab all elements using –
content = driver.find_elements_by_class_name('content')

To check practical Implementation, visit – find_elements_by_class_name() driver method – Selenium Python
With this strategy, all elements with the matching CSS selector will be returned. If no element has a matching CSS selector, a NoSuchElementException will be raised.
driver.find_elements_by_css_selector("CSS Selectors")

Example –For instance, consider this page source:
<html> <body>  <p class="content">Site content goes here.</p></body><html>
Now after you have created a driver, you can grab all elements using –
content = driver.find_elements_by_css_selector('p.content')

To check practical implementation, visit – find_elements_by_css_selector() driver method – Selenium Python
Python-selenium
selenium
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
Iterate over a list in Python