In my previous article I have written about Browsers Commands.
In a webpage there are different Web Elements present like Buttons, Inputs boxes, Links, Drop-down lists, Check-boxes, Radio buttons, Plain texts and other items.To interact with the above elements we need to locate them first. Selenium provides number of Locators to locate the elements.
The different types of locator are-
- Locating By Id
- Locating By Name
- Locating By Class Name
- Locating by Tag Name
- Locating Hyperlinks by LinkText
- Locating By Xpath
- Locating By CSS
Locating elements on a webpage can be done using the findElement and findElements method.
findElement() –
- This method will find the first element within the current page using the given “locating mechanism”.
- It returns a single WebElement.
- If “findElement” will not find any element within the page then it will throw NoSuchElementException.
- Syntax – WebElement findElement(By. “ANY LOCATOR”);
findElements() method –
- This method is use to find multiple element on webpage.
- It returns a list of WebElements.
- If no element is not found, it will return empty List of WebElement and no exception will be thrown.
- Syntax – java.util.List<WebElement> findElements(By. “ANY LOCATOR”);
Now we have to install 2 plugins[Firebug, Firepath] so as to integrate it with Firefox as it will help us to inspect the element and to generate Xpath.
- First Add Firebug to the Firefox. Click this link to Add.
- After integrating the FireBug with Firefox add FirePath from this Link.
Now Open the Firebug by Pressing F12 on your keyboard. Then click on the inspect Element icon in the Firebug. It will show the html view of that particular element. See below for the image.
Now Lets discuss the Syntax for each Locator.
- Locating Element By.Id()-
By.Id – This helps to find the element using the Id.
Syntax-
driver.findElement(By.id(“id of the element”));
- Locating Element By.Name() –
By.Name – This helps to locate the element by the value of the “name” attribute.
Syntax-
driver.findElement(By.name(“name of the element”));
- Locating Element By.tagName() –
By.tagName – This helps to locate the web element based on the HTML tag.
Syntax-
driver.findElement(By.tagName(“tagname”));
- Locating Element By.linkText() –
By.linkText – This helps to locate the element by text displayed on link.
Syntax-
driver.findElement(By.linkText(“link text”));
- Locating ElementBy.partiallinkText: It is same as Link text. Only difference is here it helps to locate the link element which is having partial matching text.
Syntax-
driver.findElement(By.partiallinkText(“partial text”));
- By.ClassName :- This helps to locate the web element based on the“class” attribute.
Syntax-
driver.findElement(By.className(“Name of the class”));
- By.xpath:- This helps to locate elements via xpath.
Syntax-
driver.findElement(By.xpath(“xpath of the element”));
- By.cssSelector:- This helps to locate element using the CSSSelector.
Syntax-
driver.findElement(By.cssSelector(“selector”));
So, in this post we discussed about the different ways of Locating Elements on a web page. Hope this article will help you 🙂