We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Use HTML classes and tags to find elements in Selenium WebDriver.
In this lesson, you’ll practice two more traditional locator types:
By.className("...")By.tagName("...")They both work directly with the HTML structure:
class attribute.input, button, mark).What it does
Finds the first element whose class attribute includes the given class name.
Example: By.className("information") → matches elements like <input class="information" />.
When to use it
By.className("btn")By.className("btn primary")What it does
Finds elements by their HTML tag name.
Example: By.tagName("input"), By.tagName("mark").
When to use it
By.tagName can return many elements. Use it when the tag is unique (e.g. <main>) or when working with a collection.Interact with the form directly to see how Selenium would locate these elements!
1234567WebElement firstInput = driver.findElement(By.className("information"));
firstInput.sendKeys("Selenium");
WebElement markElement = driver.findElement(By.tagName("mark"));
markElement.click();
driver.quit();class attribute includes information. In this practice, that is the Your Name input field."Selenium" into that input field, simulating user keyboard typing.<mark> tag on the page. Here, that is the highlighted word “Selenium”.<mark> element. The click() method simulates a mouse click on the highlighted text.By.className is great when many elements share the same style, but be aware it returns only the first match with findElement.
If you see class="btn primary", you can use By.className("btn") or By.className("primary"), but not both together with a space.
By.tagName is powerful but wide. Use it when you know the tag is unique (e.g. one <main>) or intentionally want a generic element type.