Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Locator Strategies - Selenium Python
Next article icon

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 provide 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.

After you've installed Selenium and started working with its Python module, mastering locator strategies becomes essential. These methods allow you to efficiently find and interact with single or multiple elements on a webpage, enabling you to automate tasks like clicking buttons or filling out forms. If you're looking to enhance your skills in using Selenium Python, consider exploring the Complete Guide to Software Testing & Automation by GeeksforGeeks . This course delves into advanced locator techniques and other key aspects of automation, helping you create more robust and effective test scripts.

Locator Strategies to locate multiple elements

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 -

Locators Description
find_elements (By.NAME, "name") All elements with name attribute value matching the location will be returned.
find_elements (By.XPATH, "xpath") All elements with xpath syntax matching the location will be returned.
find_elements (By.LINK_TEXT, "link text") All elements with link text value matching the location will be returned.
find_elements (By.PARTIAL_LINK_TEXT, "partial link text") All elements with partial link text value matching the location will be returned.
find_elements (By.TAG_NAME, "tag name") All elements with given tag name will be returned.
find_elements (By.CLASS_NAME, "class name") All elements with matching class attribute name will be returned.
find_elements (By.CSS_SELECTOR, "css selector") All elements with matching CSS selector will be returned.

find_elements(By.NAME)

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.

Syntax:

driver.find_elements(By.NAME, "name_of_element")

Example: For instance, consider this page source:

html
<html><body><formid="loginForm"><inputname="username"type="text"/><inputname="username"type="username"/><inputname="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

Note: command find_elements_by_name() is deprecated

find_elements(By.XPATH)

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.

Syntax:

driver.find_elements(By.XPATH, "xpath")

Example - For instance, consider this page source:

html
<html><body><formid="loginForm"><inputname="username"type="text"/><inputname="password"type="password"/><inputname="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

Note: command find_elements_by_xpath() is deprecated

find_elements(By.LINK_TEXT)

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.

Syntax:

driver.find_elements(By.LINK_TEXT, "Text of Link")

Example - For instance, consider this page source:

html
<html><body><p>Are you sure you want to do this?</p><ahref="continue.html">Continue</a><ahref="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

Note- command find_elements_by_link_text() is deprecated

find_elements(By.PARTIAL_LINK_TEXT )

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. Syntax -

driver.find_elements(By.PARTIAL_LINK_TEXT, "Text of Link")

Example - For instance, consider this page source:

html
<html><body><p>Are you sure you want to do this?</p><ahref="continue.html">Continue</a><ahref="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

Note: command find_elements_by_partial_link_text() is deprecated

find_elements(By.TAG_NAME)

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. Syntax -

driver.find_elements(By.TAG_NAME, "Tag name")

Example: For instance, consider this page source:

html
<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

Note: command find_elements_by_partial_tag_name() is deprecated

find_elements(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_elements(By.CLASS_NAME,"class_of_element")

Example - For instance, consider this page source:

html
<html><body><pclass="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

Note: command find_elements_by_class_name() is deprecated

find_elements(By.CSS_SELECTOR)

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.

Syntax:

driver.find_elements(By.CSS_SELECTOR, "CSS Selectors")

Example - For instance, consider this page source:

html
<html><body><pclass="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

Note: command find_elements_by_css_selector() is deprecated


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp