0

I am trying to get all links from page but not getting all links. I am using selenium web driver.

My code is below :

public static void main(String[] args){

    WebDriver driver = new FirefoxDriver();
    //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("http://www.digit.in");
    java.util.List<WebElement> links = driver.findElements(By.tagName("a"));
    System.out.println(links.size());
    System.out.println(links);

    } }

How can I get all links from page?

3
  • Are you getting some, but not all links, or no links at all? Are you able to find other elements?
    – Sam Woods
    Jan 23, 2015 at 18:47
  • Try This System.out.println(driver.findElement(By.tagName("a")).getSize());
    – Lav Sharma
    Mar 22, 2018 at 10:47
  • This is not printing all elements ,anyone know how to scroll all elements and get all Mar 5, 2022 at 13:12

2 Answers 2

1

There should be lot of links as result so you must put loop when getting result , See update code below :

   public static void main(String[] args){

    WebDriver driver = new FirefoxDriver();
    //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("http://www.digit.in");
    java.util.List<WebElement> links = driver.findElements(By.tagName("a"));
    System.out.println(links.size());

    for (int i=0;i<=links.size()-1;i=i+1)

    {

        System.out.println(links.get(i).getText());


    }
        }
   }
2

I believe you are actually getting all the links from the page I think what you are seeing is that when you go to print them out you are getting the String representation of the List<WebElement> collection. To actually see each of the links you need to iterate through the list.

public static void main(String[] args){

    WebDriver driver = new FirefoxDriver();
    //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("http://www.digit.in");
    java.util.List<WebElement> links = driver.findElements(By.tagName("a"));
    System.out.println(links.size());
    for(WebElement link : links)
        {
            System.out.println(link);
        }

    } 
}

This particular format will only give you the string representation of each WebElement object.

The output looks something like this:

284
[[RemoteWebDriver: firefox on XP (ef74c088-1406-44e8-a40e-ec290ea4c44a)] -> tag name: a]
[[RemoteWebDriver: firefox on XP (ef74c088-1406-44e8-a40e-ec290ea4c44a)] -> tag name: a]
[[RemoteWebDriver: firefox on XP (ef74c088-1406-44e8-a40e-ec290ea4c44a)] -> tag name: a]
[[RemoteWebDriver: firefox on XP (ef74c088-1406-44e8-a40e-ec290ea4c44a)] -> tag name: a]
[[RemoteWebDriver: firefox on XP (ef74c088-1406-44e8-a40e-ec290ea4c44a)] -> tag name: a]
[[RemoteWebDriver: firefox on XP (ef74c088-1406-44e8-a40e-ec290ea4c44a)] -> tag name: a]
....

If you want to get the say the href attribute of the WebElement you can use this instead;

System.out.println(link.getAttribute("href"));
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.