How to locate web elements in Selenium-Python?

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

1 answer to this question.

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
• 13,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
2,035 views
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
• 13,840 points
182 views
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,090 points
799 views
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
1,038 views
0 votes
1 answer