Tuesday, September 1, 2015

How to find all the links available in a page using List

This is very common interview question for every Test Automater...........so Go Through the following code to get the solution for that.


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



package mypack;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Nooflinks {
     public static WebDriver driver = new HtmlUnitDriver();
     public static void main(String[] args) {
           //open browser
           driver.get("http://www.google.com");
           //get all the list of links into a list of type webelement
           List<WebElement> link=driver.findElements(By.tagName("a"));
           //print total no of link s in page
           System.out.println(link.size());
           for(WebElement w:link){
                //display links using foreach loop
                System.out.println(w.getText());
           }
     }
}


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

I Hope this code gives you some idea.....you can find any type of tag....just replace "a" with other tags like input,div,select etc..


If you feel it is helpful....share my blog with your friends



Thankyou.................Prasad

No comments:

Post a Comment