How to use locators in Selenium Webdriver using java

Selenium WebDriver use locators to find the elements on web page with the help of findElement() and findElements() methods provided by WebDriver and WebElement class.
  • findElement() returns a WebElement object based on a specified search criteria or throw an exception if it does not find any element matching the search criteria.
  • findElements() returns a list of WebElements matching the search criteria. If no elements are found, it returns an empty list. 
The locators elements of Selenium Webdriver as run as following with java syntax and example.
1. id
2. class name
3. Linktext
4. Partial Linktext
5. Tag Name
6. Name
7. Css
8. xpath

Method: By ID
Syntax: driver.findElement(By.id(<element ID>))
Description: Locates an element using the ID attribute. The most efficient way and preferred way to locate an element on a web page is By ID. ID will be the unique on web page which can be easily identified. IDs are the safest and fastest locator option and should always be the first choice even when there are multiple choices, It is like an Employee Number or Account which will be unique.
Example:
<input type="text"  id="email" name="email" class="inputtext">
<div id="main_menu">Home.</div>

Selenium Webdriver write as:
WebElement loginemail= driver.findElement(By.id("email"));
String menuname= driver.findElement(By.id("main_menu")).getText();

driver.findElement(By.id("email")).sendKeys("abced@gmail.com");

Method: By class name
Syntax: driver.findElement(By.className(<element class>)), driver.findElements(By.className(<element class>))
Description: Locates an element using the Class attribute. There may be multiple elements with the same name. We use findElement() method for single element class name. We use findElements() for  multiple elements with the same name.
Example:
<input type="text"  id="email" name="email" class="inputtext">
<input type="password" id="pass" name="pass" class="inputtext">

or
<input name="register" class="required" type="text"/> 
<div class="mobile">8801911444444.</div>
Selenium Webdriver write as:
List<WebElement> noinputf = table.findElements(By.className("inputtext"));
or
WebElement loginemail= driver.findElement(By.className("required"));

WebElement loginemail= driver.findElement(By.className("mobile")).getText();

Method: By link text
Syntax: driver.findElement(By.linkText(<linktext>))
Description: Locates a link using link text. Make sure, there is only one unique link on the web page. If there are multiple links with the same link text (such as repeated header and footer menu links), in such cases Selenium will perform action on the first matching element with link.
Example:
<a href="http://selenium-release.storage.googleapis.com/2.45/selenium-java-2.45.0.zip">Download</a>
<a href="/projects/">Selenium Projects</a>

Selenium Webdriver write as:
WebElement download = driver.findElement(By.linkText("Downloads"));
driver.findElements(By.linkText("Selenium Projects")).click();


Method: By partial link text
Syntax: driver.findElement(By.partialLinkText(<linktext>))
Description: Locates a link using the link's partial text. It is same as By link text
Example:
<a href="http://selenium-release.storage.googleapis.com/2.45/selenium-java-2.45.0.zip">Download</a>
<a href="/projects/">Selenium Projects</a>

Selenium Webdriver write as:
WebElement download = driver.findElement(By.partialLinkText("Downloads"));
driver.findElements(By.
partialLinkText("Selenium")).click();

Method: By tag name
Syntax: driver.findElement(By.tagName(<htmltagname>))
Description: Locates an element using the HTML tag. It is very easy to handle tables,Select, and check-boxes / drop downs etc  with the help of this method.
Example:
<table style="width:100%" id="data">
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
</table> 


<a href="http://selenium-release.storage.googleapis.com/2.45/selenium-java-2.45.0.zip">Download</a>
<a href="/projects/">Selenium Projects</a> 

Selenium Webdriver write as:
WebElement table = driver.findElement(By.id("data"));
List<WebElement> row = table.findElements(By.tagName("tr"));


List<WebElement> links= driver.findElements(By.tagName("a"));
 
Method: By name
Syntax: driver.findElement(By.name(<element name>))
Description: Locates an element using the Name attribute. Name attribute can't be unique all the times. If there are multiple names, Selenium will always perform action on the first matching element.
Example:
<input type="text"  name="email" class="inputtext">
Selenium Webdriver write as:
WebElement email= driver.findElement(By.name("email"));

Method: By CSS
Syntax: driver.findElement(By.cssSelector(<css selector>))
Description: Locates an element using the CSS selector. The CSS is used as a method to provide style rules for the web pages and we can use for identifying one or more elements in the web page using css.
Example:
WebElement login = driver.findElement(By.cssSelector("input.login"));

Method: By XPath
Syntax: driver.findElement(By.xpath(<xpath>))
Description: Locates an element using XPath query. XPath is designed to allow the navigation of XML documents, with the purpose of selecting individual elements, attributes, or some other part of an XML document for specific processing.
Example:
driver.findElement(By.xpath("html/head/body/table/tr/td"));
 

20 comments:

  1. Thank you, I had been looking for some online reference to be used in my Java training and your work really helped me.
    Best Java Training institute in Chennai | Best Java Training institute in Chennai | Best Java Training institute in Chennai

    ReplyDelete
  2. Hibernate and spring are the frameworks of Java. A java developer should be well aware of these frameworks in order to master the technology and work efficeiently.
    spring training in chennai | hibernate training in chennai
    FITA Academy reviews

    ReplyDelete
  3. Java is the most robust secured and multi threaded programming language which is the reason why most the the developers go for java. A single java code can be used for various platforms.
    JAVA training in chennai | java training institutes in chennai | FITA Academy Chennai

    ReplyDelete
  4. Thanks for your information. QTP provide most precise and independent review about a software application. This automation testing tool is ideal to determine the performance and validity of a software application. Best QTP Training in Chennai | QTP training Chennai

    ReplyDelete
  5. Java is a programing language which needs no introduction. Java is immensly popular anguage which is used in building softwares in mobile app or desktop. Even today java is used to program tools like hadoop, owing to this java has becom imensley popular and one of the most preffered language around the world.
    Java training in Chennai | Java training institute in Chennai | Java course in Chennai

    ReplyDelete
  6. Hi,Thanks a lot and really happy to see such a wonderful comment.

    Selenium Training in Chennai

    ReplyDelete
  7. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
    sas training in chennai

    ReplyDelete
  8. Thanks a lot and really happy to see such a wonderful comment.
    webshere training in chennai

    ReplyDelete
  9. Nice article. Thanks for sharing. post some difference between usage of explicit wait and implicit wait. For best practice about selenium in hands on manner check out selenium training centers in chennai

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. Hi admin, your webpage is really helpful. Your coding really explains on how to use the locators in selenium with java. Continue sharing more like this.
    Regards,
    SAS Training in Chennai | SAS Course in Chennai

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. The strategy you have posted on this technology hepled me to get into the next level and had lot of informations in it. Python is one of the basic level programming and is very important one. python classes in pune

    ReplyDelete