How to locate web elements in Selenium-Python

0 votes
How to locate web elements using Selenium-Python?
May 27, 2019 in Selenium by Urvashi
14,449 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.

Hope this will help!

To learn more, go for Python Master course today.

Thank!

answered May 27, 2019 by Anvi
• 14,150 points

Related Questions In Selenium

+1 vote
1 answer
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,910 points
10,255 views
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, 2019 in Selenium by Anvi
• 14,150 points
15,199 views
0 votes
2 answers

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,500 points
12,728 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,616 views
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,910 points
6,692 views
0 votes
2 answers

How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver ?

JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy("X", " y ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
5,717 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP