Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

How to get attributes values of element in Selenium using getAttribute Method

Using getAttribute, we can extract the value of attribute of an element in Selenium WebDriver. Extracting values of attribute of element can be helpful in a number of ways. Some of the important use of getattribute method are as follows: 


  • Name of all the links or button or an object type in the Page.
  • Validating if object with particular property exists in the Page.
  • Extracting the href value for a button/link.
  • Extracting values of element of a particular class or Id. For e.g: If error message are displayed on the page with the same class. We can get all the error messages displayed getting text attribute for all element with the common class name.

Below code explains use of getattribute in Selenium Webdriver for the above purpose.

 package testproject;  
 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 testingC {  
      static WebDriver driver;  
      public static void main(String[] args) throws InterruptedException {  
           // Connect to the Internet driver server and create an instance of Internet explorer driver.       
           File file = new File("D:\\selenium\\IEDriverServer.exe");  
           System.setProperty("webdriver.ie.driver", file.getAbsolutePath());       
           try{  
                driver = new InternetExplorerDriver();  
       driver.manage().window().maximize();  
           }  
           catch(Exception e)  
           {  
                driver=new InternetExplorerDriver();  
           }  
           driver.navigate().to("https://google.com");  
           //function to get names of all the links in the Page  
           getALLNameforObject("link");  
           // function to validate a particular object with particular text appears in the Page  
           ValidateObjectExists("link", "About");       
           //   
      }  
      public static void getALLNameforObject(String strObject)  
       {  
           if(strObject.toLowerCase().trim().contentEquals("link"))  
           {  
                strObject = "a";  
           }  
           if(strObject.toLowerCase().trim().contentEquals("button"))  
           {  
                strObject = "button";  
           }  
           List<WebElement> elemLink = driver.findElements(By.tagName(strObject));  
           int intLinksinPage = elemLink.size();  
           System.out.println(intLinksinPage);  
           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 ValidateObjectExists(String strObject, String ObjName)  
       {  
           if(strObject.toLowerCase().trim().contentEquals("link"))  
           {  
                strObject = "a";  
           }  
           if(strObject.toLowerCase().trim().contentEquals("button"))  
           {  
                strObject = "button";  
           }  
           List<WebElement> elemLink = driver.findElements(By.tagName(strObject));  
           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