How to locate web elements in Selenium-Python?

0 votes
How to locate web elements using Selenium-Python?
May 27 in Selenium by Urvashi

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

@Urvashi in Python, Selenium Webdriver API supports multiple ways to identify element/s by using following strategies:

1. By ID:

Syntax:- driver.find_element_by_id()

Example: elemUserMessage = driver.find_element_by_id("user-message")

2. By Name:

Syntax:- driver.find_element_by_name()

Example: elementFormControl=driver.find_element_by_name("comment")

3. By XPath:

Syntax:- driver.find_element_by_xpath()

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

4. By ClassName:

Syntax:- driver.find_element_by_class_name()

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

5. By TagName:

Syntax: driver.find_element_by_tag_name()

Example: elemALink = driver.find_element_by_tag_name("a")

6. By CSS Selector:

Syntax:- driver.find_element_by_css_selector()

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

7. By Link Text:

Syntax:- driver.find_element_by_link_text()

Example: elemHome = browser.find_element_by_link_text("Home")

8. By Partial Link text:

Syntax:- driver.find_element_by_partial_link_text()

Example: elemContactUsLink = driver.find_element_by_partial_link_text("Contact")

To find multiple elements, you have to use find_elements_*, these methods will return a list of webelements matching with the specified locator. For eg. find_elements_* - will return you list of web elements. 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.

answered May 27 by Anvi
• 8,840 points

Related Questions In Selenium

0 votes
1 answer

How to find Elements by their Attribute in Python Selenium WebDriver

To find all the <td> tags which have attribute data-sortable-id you ...READ MORE

answered Jul 19, 2018 in Selenium by Samarpit
• 5,130 points
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Installing Selenium Webdriver with Python package

Hey Hemant, for installing Selenium Webdriver with ...READ MORE

answered May 8 in Selenium by Anvi
• 8,840 points
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,050 points
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
0 votes
1 answer
0 votes
1 answer

How to mouse hover on a web element using Selenium WebDriver?

Hello Nitin, to automate mouse hovering over ...READ MORE

answered May 7 in Selenium by Anvi
• 8,840 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.