Locating Strategies - Selenium IDE
Locators are elements or attributes of a web page that help to identify the elements on which actions needs to be performed. Selenium IDE provides various methods to locate elements in a web page using different strategies.
Syntax
driver.find_element_by_<method>('value')
where,
<method>
can be any of the given strategies.value
can be a string representing the value for the given strategy.
Example
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.google.com")
search_box = driver.find_element_by_name("q")
search_box.send_keys("selenium")
Explanation
In the above example, find_element_by_name()
method is used to locate the element with name attribute as 'q' i.e the search box element in the web page. After locating the element, the send_keys()
method is used to enter the text 'selenium' in the search box.
Strategies for Locating Elements
Selenium IDE provides the following strategies to locate web elements:
- find_element_by_id(id) - Locates the element by its unique ID attribute.
- find_element_by_name(name) - Locates the element by its name attribute.
- find_element_by_xpath(xpath) - Locates the element by its XPath.
- find_element_by_link_text(link_text) - Locates the anchor element (a) by its exact text.
- find_element_by_partial_link_text(link_text) - Locates the anchor element (a) by its partial text.
- find_element_by_tag_name(tag_name) - Locates the element by its tag name.
- find_element_by_class_name(class_name) - Locates the element by its class name.
- find_element_by_css_selector(css_selector) - Locates the element by its CSS selector.
Use
Locating web page elements is the foundation of test automation, and it is essential to choose the right locator according to the situation. Below are the cases where the specific locator strategy is useful:
- find_element_by_id(): When the element has a unique ID attribute on the web page.
- find_element_by_name(): When the target element has the name attribute on the web page.
- find_element_by_link_text(): When finding a link element anchored by the exact text.
- find_element_by_partial_link_text(): When finding a link element anchored by partial text.
- find_element_by_tag_name(): When finding an element type that is not duplicated.
- find_element_by_class_name(): When the element has the class attribute and the class attribute value is unique.
- find_element_by_css_selector(): When the element has multiple attributes or there is no specific attribute available, CSS Selector can be used.
Important Points
- It is recommended to use reliable and unique attribute selectors when using locators.
- When using the find_element_by_id() method, make sure that the ID attribute is unique on the page.
- Best Practice is to use one class attribute value for a web element.
- tag_name cannot be relied on if there are multiple occurrences of the same element type.
- If the same element has multiple classes, the class that is unique should be selected.
Summary
This page provides an introduction to different strategies to locate elements in a web page. Usage of these locators plays a crucial role in test automation as the test scripts perform different actions on these elements. Therefore, it's essential to choose the suitable strategy for locating.