[TOC]
Web element interactions are at the core of web automation using Selenium and Python. By leveraging the various locator strategies available, you can precisely locate and interact with web elements. Modern websites can be very complex and unique, therefore learning different locators can be a great investment.
Benefits of knowing various locator strategies:
- Precise Element Identification: Locators allow you to target specific elements on a web page accurately.
- Flexibility and Adaptability: Locators provide flexibility in locating elements by offering different strategies such as ID, class name, tag name, etc.
- Maintainability and Robustness: Locators help in creating maintainable and robust automation scripts.
- Reusability: Once you define a locator for an element, you can reuse it across multiple test cases or scenarios.
- Improved Readability: Locators make your automation scripts more readable and understandable.
- Enhanced Debugging and Troubleshooting: When an automation script encounters an issue, having well-defined locators makes debugging and troubleshooting more manageable.
- Cross-Browser and Platform Compatibility: Locators help ensure cross-browser and platform compatibility.
- Reduced Maintenance Effort: With reliable locators, your automation scripts require fewer updates and maintenance efforts.
Locator strategies
As mentioned before, Selenium comes with many locators, such as:
- ID locator,
- class name locator,
- tag name locator,
- name locator,
- link text locator,
- partial link text locator,
- CSS Selector locator.
It is even possible to combine these locators with additional filters/values to make the selecting even more precise and specific for the application.
ID Locator
- Syntax:
driver.find_element(By.ID, 'element_id')
- Description: Locates an element using its unique identifier (id attribute).
Example: To locate a login button with an ID attribute of “login-btn”, you can use the following code snippet:
|
|
Class name Locator
- Syntax:
driver.find_element(By.CLASS_NAME, 'class_name')
- Description: Locates an element using its class name.
Example: To locate an element with a class name of “menu-item”, you can use the following code snippet:
|
|
Tag name Locator
- Syntax:
driver.find_element(By.TAG_NAME, 'tag_name')
- Description: Locates an element using its HTML tag name.
Example: To locate the first paragraph element (<p>
) on a webpage, you can use the following code snippet:
|
|
Name Locator
- Syntax:
driver.find_element(By.NAME, 'element_name')
- Description: Locates an element using its name attribute.
Example: To locate an input field with a name attribute of “email”, you can use the following code snippet:
|
|
Link Text Locator
- Syntax:
driver.find_element(By.LINK_TEXT, 'link_text')
- Description: Locates a link element using the exact text of the link.
Example: To locate a link with the exact text “About Us”, you can use the following code snippet:
|
|
Partial Link Text Locator
- Syntax:
driver.find_element(By.PARTIAL_LINK_TEXT, 'partial_link_text')
- Description: Locates a link element using a partial match of the link text.
Example: To locate a link with partial text “Privacy Policy”, you can use the following code snippet:
|
|
CSS Selector Locator
- Syntax:
driver.find_element(By.CSS_SELECTOR, 'css_selector')
- Description: Locates an element using a CSS selector.
Example: To locate an element with the CSS selector .login-form .username-input
, you can use the following code snippet:
|
|
XPath Locator
- Syntax:
driver.find_element(By.XPATH, 'xpath_expression')
- Description: Locates an element using an XPath expression.
Example: To locate an element with the XPath //div[@class='container']//input[@name='username']
, you can use the following code snippet:
|
|