Day 5 of 100daysofcode : So, how do locators really work using Selenium?
Locators in Selenium work by identifying elements on a webpage using their unique attributes or properties, such as id
, name
, class
, or tag structure.
Selenium communicates with the browser’s DOM (Document Object Model) to search for elements that match the specified locator. Once an element is located, Selenium enables interactions like clicking, typing, or extracting data.
By combining locators with Selenium’s powerful methods, testers can precisely target elements even on dynamic and complex web pages.
1. ID Locator
- Description: Uses the
id
attribute of an HTML element. - Syntax:
element = driver.find_element(By.ID, "element_id")
- Advantages:
- Unique within a page.
- Fast and reliable.
2. Name Locator
- Description: Uses the
name
attribute of an HTML element. - Syntax:
element = driver.find_element(By.NAME, "element_name")
- Advantages:
- Simple and effective if
name
attributes are unique.
- Simple and effective if
- Disadvantages:
- May not always be unique.
3. Class Name Locator
- Description: Uses the
class
attribute of an HTML element. - Syntax:
element = driver.find_element(By.CLASS_NAME, "class_name")
- Advantages:
- Works well for elements with specific styling.
- Disadvantages:
- Fails when multiple elements share the same class.
4. Tag Name Locator
- Description: Uses the HTML tag name (e.g.,
input
,button
). - Syntax:
element = driver.find_element(By.TAG_NAME, "tag_name")
- Use Case:
- Retrieving collections of elements like all links (
<a>
tags) on a page.
- Retrieving collections of elements like all links (
5. Link Text Locator
- Description: Locates links based on their text.
- Syntax:
element = driver.find_element(By.LINK_TEXT, "Link Text")
- Use Case:
- Ideal for links with unique text.
- Disadvantages:
- May not work well with long or dynamic text.
6. Partial Link Text Locator
- Description: Locates links using partial text match.
- Syntax:
element = driver.find_element(By.PARTIAL_LINK_TEXT, "Partial Text")
- Use Case:
- Useful when link text is too long or dynamic.
7. CSS Selector
- Description: Uses CSS selectors to locate elements.
- Syntax:
element = driver.find_element(By.CSS_SELECTOR, "css_selector")
-
Advantages:
- Extremely flexible and powerful.
- Supports complex matching patterns.
-
Examples:
- By class:
".classname"
- By ID:
"#id"
- By attribute:
[type='submit']
- By class:
Locators in Selenium are used to identify web elements on a webpage. They are a fundamental part of Selenium’s interaction with web applications. Here’s a detailed overview of the commonly used locators in Selenium:
from selenium import webdriver
from selenium.webdriver.common.by import By
# Initialize the WebDriver
driver = webdriver.Chrome()
# Open the practice login page
driver.get("https://practicetestautomation.com/practice-test-login/")
# Locate the username and password fields
username_field = driver.find_element(By.ID, "username")
password_field = driver.find_element(By.ID, "password")
# Enter credentials
username_field.send_keys("student")
password_field.send_keys("Password123")
# Locate and click the login button using a CSS Selector
login_button = driver.find_element(By.CSS_SELECTOR, "button[type='submit']")
login_button.click()
# Validate login by checking the URL or a success message
success_message = driver.find_element(By.TAG_NAME, "h1")
assert success_message.text == "Logged In Successfully", "Login failed!"
# Close the browser
driver.quit()