@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!