Class Name and Tag Name Locators in Selenium Python
Selenium is a powerful tool for automating web browsers, and locators are at the heart of interacting with web elements. Two commonly used locators in Selenium are class name and tag name. These are useful when you want to interact with web elements that share common styling (class name) or HTML structure (tag name). In this blog, we’ll explore how to use class name and tag name locators in Selenium with Python, including real-world examples and best practices.
What is a Locator in Selenium?
A locator is a way to identify HTML elements on a web page. Selenium provides several types of locators like id, name, class_name, tag_name, xpath, and css_selector. Choosing the right locator is key to writing stable and efficient test scripts.
Using Class Name Locator
The class_name locator allows you to target elements by their CSS class. It's particularly useful when IDs are not available and multiple elements share the same class.
Example HTML:
html
<div class="alert">Warning!</div>
<button class="btn primary">Submit</button>
Python Code:
python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
# Locate single element by class name
alert = driver.find_element("class name", "alert")
print(alert.text)
# Locate multiple elements with same class
buttons = driver.find_elements("class name", "btn")
for button in buttons:
print(button.text)
driver.quit()
Note: If an element has multiple classes, only use one class name at a time. For example, "btn primary" will not work — use either "btn" or "primary".
Using Tag Name Locator
The tag_name locator is used to find elements based on their HTML tag, such as div, p, input, a, button, etc. It is especially helpful when you want to count elements or iterate over groups of similar tags.
Example HTML:
html
<ul>
<li>Apple</li>
<li>Banana</li>
<li>Cherry</li>
</ul>
Python Code:
python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
# Locate all list items
list_items = driver.find_elements("tag name", "li")
for item in list_items:
print(item.text)
driver.quit()
This locator is also useful when dealing with forms or table rows, where multiple elements share the same tag.
When to Use Class Name and Tag Name
Use class_name when:
Elements have a unique or consistent class.
Styling classes are applied and reused.
Use tag_name when:
You want to grab all elements of a certain type.
You are counting elements or scraping text content.
Best Practices
- Avoid overly generic tags: For example, div and span might be too broad.
- Be specific with class names: Use unique or less common classes when possible.
- Combine locators when needed: Use xpath or css_selector if class_name or tag_name is not specific enough.
- Use find_elements for multiple matches: It returns a list even if only one match is found.
Conclusion
Class name and tag name locators in Selenium with Python offer simple and readable ways to interact with elements. While they are not always the most specific options, they can be very effective in many real-world scenarios. By understanding when and how to use these locators, you can write more efficient and maintainable Selenium scripts.
Learn Selenium with Pyhton Training Hyderabad
Read More: XPath in Selenium Python: Basics and UsageVisit IHUB Talent Institute Hyderabad
Get Direction
Comments
Post a Comment