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:
- driver.get("http://facebook.com");
- List <webelement> totalLinks=driver.findElements(By.tagName("a"));
- System.out.println("total links "+totalLinks.size());
- </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:
- driver.get("http://facebook.com");
- List<webelement> checkboxes=driver.findElements(By.xpath("//input[@type='checkbox']"));
- System.out.println("total checkboes "+checkboxes.size());
- </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:
- driver.get("http://facebook.com");
- List<webelement> dropdown=driver.findElements(By.tagName("select"));
- System.out.println("total dropdown lists "+dropdown.size());
- </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:
- driver.get("http://facebook.com");
- List <webelement> textboxes=driver.findElements(By.xpath("//input[@type='text'[@class='inputtext']"));
- System.out.println("total textboxes "+textboxes.size());
- </webelement>
Here is the sample code to get the total no.of textboxes on hotmail registration page:
- driver.get("https://signup.live.com");
- List <webelement> totalTextboxes=driver.findElements(By.xpath("//input[@type='text']"));
- System.out.println("total textboxes "totalTextboxes.size());
- </webelement>
Getting total no.of iframes on a Webpage :
Here is the sample code to get the total no.of iframes :
- driver.get("https://www.facebook.com/googlechrome/app_158587972131");
- List <webelement> totaliFrames=driver.findElements(By.tagName("iframe"));
- System.out.println("total links "+totaliFrames.size());
- </webelement>
When I run this code every time it shows number of elements is 1.. No of checkboxes=1 like this.
ReplyDeleteHi Pranav,
ReplyDeleteCan You Post your peace of code...