HTML 5 : Understanding HTML for Locating Elements in Selenium
To help you understand locators in Selenium, it's crucial to grasp more detailed HTML concepts. Selenium uses HTML tags and attributes to find (or "locate") elements on a web page. Below is an expanded article section with HTML knowledge specifically useful for identifying and locating elements in Selenium.
๐ Understanding HTML for Locating Elements in Selenium
Selenium interacts with web elements using locators, which are based on HTML structure and attributes. Here's what you need to know about HTML to use Selenium effectively:
1. ๐ Using the id
Attribute
id
is a unique identifier for an HTML element — the most preferred locator.
<input type="text" id="username">
Selenium Locator Example:
driver.findElement(By.id("username"));
2. ๐จ๐จ๐ง๐ฆ Using the class
Attribute
class
is used to apply styles, and can also be used to locate elements (but it's not unique).
<button class="btn login">Login</button>
Selenium Locator Example:
driver.findElement(By.className("btn"));
3. ✏️ Using the name
Attribute
Often used in forms — good for locating input fields.
<input type="email" name="userEmail">
Selenium Locator Example:
driver.findElement(By.name("userEmail"));
4. ๐งฑ HTML Tag Names
Common tags used with Selenium include:
-
<input>
– for text fields, checkboxes, radios -
<button>
– for buttons -
<a>
– for links -
<select>
– for dropdowns -
<textarea>
– for multiline input
Selenium Locator Example:
driver.findElements(By.tagName("input"));
5. ๐ Using Link Text and Partial Link Text
For clickable links (<a>
tags):
<a href="/register">Register Now</a>
Selenium Locator Examples:
driver.findElement(By.linkText("Register Now"));
driver.findElement(By.partialLinkText("Register"));
6. ๐งฉ Using Attributes in XPath/CSS Selectors
Many times, you’ll locate elements using XPath or CSS Selectors based on attributes.
HTML Example:
<input type="text" id="email" name="userEmail" placeholder="Enter email">
XPath:
driver.findElement(By.xpath("//input[@id='email']"));
driver.findElement(By.xpath("//input[@placeholder='Enter email']"));
CSS Selector:
driver.findElement(By.cssSelector("input#email")); // Using id
driver.findElement(By.cssSelector("input[name='userEmail']")); // Using attribute
7. ๐ฆ HTML Element Hierarchy
Understanding how elements are nested in HTML helps you write relative XPath.
<div class="form">
<label>Email</label>
<input type="text" name="email">
</div>
Relative XPath:
driver.findElement(By.xpath("//div[@class='form']/input"));
8. ๐ Radio Buttons and Checkboxes
Use type="radio"
or type="checkbox"
to identify these inputs.
<input type="radio" name="gender" value="male"> Male
<input type="checkbox" name="terms"> I agree to terms
Selenium:
driver.findElement(By.cssSelector("input[type='radio'][value='male']")).click();
9. ⬇️ Dropdowns (<select>
Tag)
Dropdowns use <select>
and <option>
.
<select id="country">
<option value="IN">India</option>
<option value="US">USA</option>
</select>
Selenium (Java + Select Class):
Select country = new Select(driver.findElement(By.id("country")));
country.selectByValue("IN");
10. ๐ฝ Hidden and Dynamic Elements
Some elements are dynamically shown or hidden with JavaScript.
<div id="dropdown" style="display: none;">Options</div>
You may need to use JavaScript Executor or waits in Selenium to handle these.
Summary Table of HTML Attributes and Selenium Locators
HTML Attribute | Selenium Locator | Usage Example |
---|---|---|
id |
By.id() |
By.id("loginBtn") |
class |
By.className() |
By.className("form-group") |
name |
By.name() |
By.name("email") |
any attribute | By.xpath() |
By.xpath("//input[@placeholder='Email']") |
tag only | By.tagName() |
By.tagName("input") |
visible text | By.linkText() |
By.linkText("Register Now") |
partial text | By.partialLinkText() |
By.partialLinkText("Register") |
CSS attribute | By.cssSelector() |
By.cssSelector("input[name='email']") |
✨ Pro Tip
Use browser developer tools (F12) to inspect elements, view their attributes, and test XPath/CSS expressions in the console using:
$x("//input[@type='text']")
document.querySelector("input#email")
Comments
Post a Comment