Selenium By Class
Introduction to By Class
The By class in Selenium is used to locate elements on a web page. It provides various methods for locating elements, such as by ID, name, class name, tag name, link text, partial link text, CSS selector, and XPath. In previous tutorial, we saw that By class object is passed as an argument to the findElement/findElements method of the WebDriver interface to locate or reach to elements on a web page.By class usage syntax
The By class is passed as an argument to the findElement/findElements method of the WebDriver interface to locate elements on a web page. The syntax for using the By class to locate elements on a web page is as follows:-
With findElement method:
driver.findElement(By.id("elementId"));
-
With findElements method:
driver.findElements(By.className("className"));
Tip: By is a static class in
Selenium,
so we can call its methods
directly without creating an
object of the By class like
By.id("elementId")
.
By Methods Details
We will see the details of each By class method:-
By.id(String id): This method locates
HTML
elements by the value of their "id" attribute.HTML Element
If we have any html element with element id like below:<input id="elementId" type="text" />
Selenium Code
then we can locate this element using below code:driver.findElement(By.id("elementId"));
-
By.name(String name): This method locates
HTML
elements by the value of their "name" attribute.HTML Element
If we have any html element with element name like below:<input name="elementName" type="text" />
Selenium Code
then we can locate this element using below code:driver.findElement(By.name("elementName"));
-
By.className(String className): This method locates
HTML
elements by the value of their "class" attribute.HTML Element
If we have any html element with element class like below:<input class="className" type="text" />
Selenium Code
then we can locate this element using below code:driver.findElement(By.className("className"));
-
By.tagName(String tagName): This method locates
HTML
elements by their tag name.HTML Element
If we have any html element with element tag name like below:<input type="text" />
Selenium Code
then we can locate this element using below code:driver.findElement(By.tagName("tagName"));
-
By.linkText(String linkText): This method locates
HTML
anchor elements by the exact text they display.HTML Element
If we have any html element with element link text like below:<a href="https://nicetesters.com">NiceTesters</a>
Selenium Code
then we can locate this element using below code:driver.findElement(By.linkText("NiceTesters"));
-
By.partialLinkText(String partialLinkText): This method
locates
HTML
anchor elements by a partial match of the text they display.HTML Element
If we have any html element with element partial link text like below:<a href="https://nicetesters.com">NiceTesters</a>
Selenium Code
then we can locate this element using below code:driver.findElement(By.partialLinkText("Nice"));
-
By.cssSelector(String selector): This method locates
HTML
elements by a CSS selector.HTML Element
If we have any html element with element css selector like below:<input type="text" class="className" />
Selenium Code
then we can locate this element using below code:
In this we are using dot(.) before the class name to locate the element based on class name.driver.findElement(By.cssSelector(".className"));
-
By.xpath(String xpath): This method locates
HTML
elements by an XPath expression.HTML Element
If we have any html element with element xpath like below:<input type="text" />
Selenium Code
then we can locate this element using below code:
In this we are using double forward slash(//) before the tag name to locate the element based on tag name. This is the relative xpath expression.driver.findElement(By.xpath("//input"));
By Class code Example
We will see a simple example of using the By class to locate elements on a web page.HTML Code
Suppose we have the following HTML code:<button id="elementId">Click Me</button>
Selenium Code
We can locate the above element using the By.id method as follows:WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
WebElement element = driver.findElement(By.id("elementId"));
element.click();
Method | Description |
---|---|
By.id(String id) |
Locates elements by the value of their "id" attribute. |
By.name(String name) |
Locates elements by the value of their "name" attribute. |
By.className(String className) |
Locates elements by the value of their "class" attribute. |
By.tagName(String tagName) |
Locates elements by their tag name. |
By.linkText(String linkText) |
Locates anchor elements by the exact text they display. |
By.partialLinkText(String partialLinkText) |
Locates anchor elements by a substring of the text they display. |
By.cssSelector(String selector) |
Locates elements by a CSS selector. |
By.xpath(String xpath) |
Locates elements by an XPath expression. |
Next, we will learn about the WebElement
Interface in Selenium WebDriver.