Sunday, January 31, 2016

GetAttributes, Title, Text and more.... in Selenium Webdriver

In this article, we will discuss how to extract data from elements or Page using Selenium Webdriver. Extracting attribute information and text of elements helps to validate elements have correct and expected attribute value or text.

Problem Statement of this article: 
  • How to get title of the page?
  • How to get text of an element?
  • How to get attribute value of element?

How to get title of the Page?


We can get title of the page as well as url of the current Page as shown  below: 
 WebDriver driver = new FirefoxDriver();  
 driver.navigate().to("https://www.google.com");  
 // We can get title of the page using driver.getTitle  
 System.out.println(driver.getTitle());  
 // We can get url of the page using driver.getCurrentUrl  
 System.out.println(driver.getCurrentUrl()); 

How to get text of an element in the Page?


We can get text of an element in the page using gettext() as shown in the code below:
One of the useful and important use of getText can be to get error messages displayed in page, where the class name of all the error message displayed in the page is same. It can be implemented as follows:
  • Identify the classname of the error message.
  • Using findelements, find collection of elements with class name.
  • Loop through the element collection and getText of each element.
 WebDriver driver = new FirefoxDriver();  
 driver.navigate().to("https://www.google.com");  
 WebElement elem = driver.findElement(By.id("gb_P gb_R"));  
 String message = elem.getText();  


How to use getAttribute to validate existence of an object and validating attribute properties?


Below code explains how to use getattribute to validate existence of element and extracting attribute value for the object.
 import java.io.File;  
 import java.util.List;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.ie.InternetExplorerDriver;  
 
 
 public class FindAttributes {  
      static WebDriver driver;  
      @Test
  public void testing()
  { 
   driver = new FirefoxDriver();
   driver.navigate().to("https://www.google.com");
           //calling method to get names of all the links in the Page  
           textForObjectType("link");  
           // function to validate a particular object with particular text exists in the Page  
           ValidateObjectText("link", "About");       
           //   
      }  
      public static void textForObjectType(String strObj)  
       {  
           if(strObj.toLowerCase().trim().contentEquals("link"))  
           {  
                strObj = "a";  
           }  
           if(strObj.toLowerCase().trim().contentEquals("button"))  
           {  
                strObj = "button";  
           }  
     // Similarly can add object of other types. can use enum and switch for better control.
           List<WebElement> elemLink = driver.findElements(By.tagName(strObj));  
           int intLinksinPage = elemLink.size();  
           System.out.println(intLinksinPage);  
     // Loop to get attribute value of all the objects of the type in the page
           for (int i = 0;i<intLinksinPage;i++)  
           {  
                System.out.println("The name of the link " + (i+1) +" in the page is :- " + elemLink.get(i).getAttribute("text"));  
           }  
      }  
      public static void ValidateObjectText(String strObj, String ObjName)  
       {  
           if(strObj.toLowerCase().trim().contentEquals("link"))  
           {  
                strObj = "a";  
           }  
           if(strObj.toLowerCase().trim().contentEquals("button"))  
           {  
                strObj = "button";  
           }  
           List<WebElement> elemLink = driver.findElements(By.tagName(strObj));  
           int intLinksinPage = elemLink.size();  
           System.out.println(intLinksinPage);  
           for (int i = 0;i<intLinksinPage;i++)  
           {  
                if(elemLink.get(i).getAttribute("text").contentEquals(ObjName))  
                {  
                     System.out.println("Link exists in Page with text " + elemLink.get(i).getAttribute("text"));  
                }  
           }  
      }  
 }

No comments:

Post a Comment