Wednesday 28 February 2018

Different Locators in Selenium

Locators is most important part of selenium automation because based on this we are going to find element on tha pgae. So, it locators failed to find element then our script will fail.

sometime locate accurate GUI element on page is very difficult task. Hence, Selenium provide many type of locators and we can use to find GUI element on page.

Below are different type of locators available in selenium.

  • ID
  • Name
  • Link Text
  • DOM
  • CSS Selector
  • Xpath

Locating By ID:

This locator technique is most common way to find element. Please see below image. we can find Google search text box with this.



Syntax:

driver.findElement(By.id("lst-ib"));


Locating By Name:

Locating by Name is same as Locating By ID just we have to use 'Name' attribute of tag instead of 'ID'.




Syntax:

driver.findElement(By.name("q"));



Locating By Link Text:

This technique is used only for finding link element on the page. It can not be used for text area, button or other type of element.





Syntax:

driver.findElement(By.linkText("Selenium - Web Browser Automation"));



Locating By CSS Selector:

CSS Selector is patterns used to find an element with combination of tag, id, class, and attributes. Locating by CSS Selector is more complicated than the other methods. It is very popular when element has no id. we can find element using tag and class.

1. Tag and ID



Syntax:

driver.findElement(By.cssSelector("input#lst-ib"));

# sign use for indicate ID.

2. Tag and Class



Syntax:

driver.findElement(By.cssSelector("input.gsfi"));

. sign use for indicate Class.

3. Tag and Attribute


Syntax:

driver.findElement(By.cssSelector("input[name=q]"));

4. Tag, Class and Attribute


Syntax:

driver.findElement(By.cssSelector("input.gsfi[name=q]"));


Locating By DOM:

The Document Object Model (DOM), is the way in which HTML elements are structured. Selenium is able to use the DOM in accessing page elements. We can use ID, Name for locating element with DOM

1. ID



Syntax:

document.getElementById("lst-ib");

2. Name


Syntax:

document.getElementsByName("q");

If there is multiple elements with same name then selenium will locate first element. If we want to locate second element the we have to use index. Please see below syntax.

document.getElementsByName("q")[2];


Locating By XPath:

Xpath is my favorite locators among all. 
Xpath is used XML for locating elements. Xpath is most common and widely used for locating elements. 



Syntax:

driver.findElement(By.xpath("//input[@id = 'lst-ib']"));

We can use different locators technique simultaneously in one locators.
For ex. If we want to find button which has id = abc and value = xyz then we can do with Xpath.

driver.findElement(By.xpath("//input[(@id = 'abc') and (@value = 'xyz')]"));

You can find xpath of any element with chrome browser easily. Follow below steps for find xpath.

1. Righ click on that element.
2. Click on inspect element
3. Right click on highlighted part
4. Go to Copy Option
5. Click on Copy Xpath and paste it to Notepad




How to Highlight Element in Selenium

When we click on any element in automation script then sometimes script is so fast that we can not recognize that which element is clicked.

So, we can do one thing that before click on any element we can highlight that element then click on that element. So, that we can recognize that which element is clicked.

We can not directly highlight element using selenium. We need to use Java script for highlight that element. If we want to execute java script then selenium driver directly can not execute java script. We have to use 'JavascriptExecutor' for execute java script with web driver.

I have created two function one for only highlight element and second one is for highlight element and lick on that element.

First function actually works like blinking on that element.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public void mHighlightElement(WebDriver driver, WebElement vElement)
    {     
     try
     {
      //Creating JavaScriptExecuter Interface
      JavascriptExecutor js = (JavascriptExecutor)driver;      
      for (int i=0; i<3; i++)
      {       
              js.executeScript("arguments[0].style.border='4px groove blue'", vElement);
              Thread.sleep(1000); // wait for see blinking
              js.executeScript("arguments[0].style.border=''", vElement);
      }
       
     }
     catch (Exception E)
     {
   System.out.println("Error in Highlight element");
  }
    }

Second Function


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public void mHighlightAndClick(WebDriver driver, WebElement vElement)
    {
     JavascriptExecutor js = (JavascriptExecutor)driver;
     try
     {
       js.executeScript("arguments[0].style.border='4px groove blue'", vElement);
          Thread.sleep(1000);
          
          vElement.click();                             
     }
     catch(Exception E)
     {
      System.out.println("Error in Highlight and click on element");
     }
    }


Please comment if you have any question.


Popular