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?

  • Are you getting some, but not all links, or no links at all? Are you able to find other elements? – Sam Woods Jan 23 '15 at 18:47
  • Try This System.out.println(driver.findElement(By.tagName("a")).getSize()); – Lav Sharma Mar 22 '18 at 10:47
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"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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