As we all knows, Each and every software web application contains many number of different links/URLs. Some of them are redirecting to some page of same website and others are redirecting to any external software web application. One of my blog reader has requested the webdriver software test script example for extracting all these links from page and print it in console using selenium webdriver. I have created and published many different WEBDRIVER EXAMPLE SCRIPTS on this blog but requested example by blog reader is not pushed till now so I have created example for the same to share with all of you.
You can look at example of EXTRACTING TABLE DATA USING SELENIUM WEBDRIVER.
We have seen HOW TO USE findElements in webdriver to locate multiple elements of software web page. We can use same thing here to locate multiple links of the page. Bellow given example will retrieve all URLs from the software web page and will print all of them in console. You can click on each URL if you wish to do so. Try bellow given example for different websites by providing that website's URL in @Before annotation.
@Test
public void test () throws InterruptedException
{
try {
List<WebElement> no = driver.findElements(By.tagName("a"));
int nooflinks = no.size();
System.out.println(nooflinks);
for (WebElement pagelink : no)
{
String linktext = pagelink.getText();
String link = pagelink.getAttribute("href");
System.out.println(linktext+" ->");
System.out.println(link);
}
}catch (Exception e){
System.out.println("error "+e);
}
}
THIS LINK will show you many other basic action command examples. Have you READ POST on how to find broken link from page?
Thanks for very helpful note.
ReplyDeleteBut, how could i clicking all links one by one after extracting??
Now that you have the all the link texts you can search for all of them with findElement(By.linkText()) method and click on all the links.
ReplyDeleteI did not understand for (WebElement pagelink : no)? What is the usage of :....can you please add comment for all the steps and explain the functionality. I am new to Selenium.
ReplyDeleteThis is advanced for loop
Deleteuse can use
for(i=0;i<links.size();i++)
{
}
for(WebElement pagelink : no) - This is same as for for loop used to iterate over each and every elements of List no
ReplyDeleteHow can we retrieve links for the entire site instead of only a single page?
ReplyDelete