Master Selenium Locators: name, id, tagName, className, linkText, partialLinkText
This guide shows how to use Selenium WebDriver in Java to locate web elements by name, id, tag name, class name, link text, and partial link text, providing complete example code for each method to interact with input fields and links on a test page.
By.name()
Demonstrates locating an input element by its name attribute and sending a value using Selenium WebDriver in Java.
public class SearchTextByName {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://yy3.preview.bicaijia.com/login.do");
driver.findElement(By.name("code")).clear();
driver.findElement(By.name("code")).sendKeys("WHBOXEntity002");
}
}By.id()
Shows how to locate the same input element using its id attribute and input a new value.
public class SearchTextById {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://yy3.preview.bicaijia.com/login.do");
driver.findElement(By.id("code")).clear();
driver.findElement(By.id("code")).sendKeys("WHBOXEntity002");
}
}By.tagName()
Illustrates finding all button elements on the page by their tag name and printing the count.
public class SearchByTagName {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://yy3.preview.bicaijia.com/login.do");
List<WebElement> clickButtons = driver.findElements(By.tagName("button"));
System.out.println(clickButtons.size()); // prints number of buttons
}
}By.className()
Uses the element's CSS class name ( form-control) to locate an input field and send text.
public class SearchByClassName {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://yy3.preview.bicaijia.com/login.do");
WebElement searchBox = driver.findElement(By.className("form-control"));
searchBox.sendKeys("Hello,world");
}
}By.linkText()
Finds a hyperlink by its exact visible text ("详情") and clicks it.
public class SearchByLinkText {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://yy3.preview.bicaijia.com/login.do");
WebElement searchLink = driver.findElement(By.linkText("详情"));
searchLink.click();
}
}By.partialLinkText()
Locates a hyperlink using a partial match of its text (e.g., "详") when the full text is unknown, then clicks it.
public class SearchByPartialLinkText {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://yy3.preview.bicaijia.com/login.do");
WebElement searchLink = driver.findElement(By.partialLinkText("详"));
searchLink.click();
}
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
