How to Get Webpage Links Using Selenium Example Program?

How to Get Webpage Links Using Selenium Example Program?

package com.selenium.basics;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class RetriveAllLinks 
{
   @org.testng.annotations.Test
   public void importlink() throws InterruptedException
   {
      WebDriver driver=new FirefoxDriver();
      driver.get("http://www.softwaretestingblog.in/");
      Thread.sleep(3000);
      List<WebElement> li=driver.findElements(By.tagName("a"));
      System.out.println(li.size());
      for(int i=1;i<li.size();i++)
      {
         String t1=li.get(i).getText();
         String t2=li.get(i).getAttribute("href");
         System.out.println(t1+" --- >> "+t2);			
      }	
      driver.close();
   }
}

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment