Wednesday 3 July 2013

Getting total no.of Checkboxes/Textboxes/Dropdowns/iframes/Links on a web page




In one of the Previous Post we had details about WebDriver findElements() method. This is very useful to find the total no.of  desired elements on a wepage .

Here are some of the examples :

Getting total no.of Links on a Webpage :
Here is the sample code to get the total no.of links on facebook registration page:
  1. driver.get("http://facebook.com");  
  2. List <webelement> totalLinks=driver.findElements(By.tagName("a"));  
  3. System.out.println("total links "+totalLinks.size());  
  4. </webelement>  

Getting total no.of checkboxes on a Webpage :
Here is the sample code to get the total no.of checkboxes on facebook registration page:
  1. driver.get("http://facebook.com");  
  2. List<webelement> checkboxes=driver.findElements(By.xpath("//input[@type='checkbox']"));  
  3. System.out.println("total checkboes "+checkboxes.size());  
  4. </webelement>  



Getting total no.of dropdown menus on a Webpage :
Here is the sample code to get the total no.of dropdown menus on a facebook registration page:
  1. driver.get("http://facebook.com");  
  2. List<webelement> dropdown=driver.findElements(By.tagName("select"));  
  3. System.out.println("total dropdown lists "+dropdown.size());  
  4. </webelement>  



Getting total no.of textboxes on a Webpage :
Here is the sample code to get the total no.of textboxes on facebook registration page:
  1. driver.get("http://facebook.com");  
  2. List <webelement> textboxes=driver.findElements(By.xpath("//input[@type='text'[@class='inputtext']"));  
  3. System.out.println("total textboxes "+textboxes.size());  
  4. </webelement>  

Here is the sample code to get the total no.of textboxes on hotmail registration page:
  1. driver.get("https://signup.live.com");  
  2. List <webelement> totalTextboxes=driver.findElements(By.xpath("//input[@type='text']"));  
  3. System.out.println("total textboxes "totalTextboxes.size());  
  4. </webelement>  

Getting total no.of iframes on a Webpage :
Here is the sample code to get the total no.of iframes :
  1. driver.get("https://www.facebook.com/googlechrome/app_158587972131");  
  2. List <webelement> totaliFrames=driver.findElements(By.tagName("iframe"));  
  3. System.out.println("total links "+totaliFrames.size());  
  4. </webelement>  

2 comments:

  1. When I run this code every time it shows number of elements is 1.. No of checkboxes=1 like this.

    ReplyDelete
  2. Hi Pranav,

    Can You Post your peace of code...

    ReplyDelete

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...