3

I'm trying to get all links from a web page. I tried to use

WebDriver driver = FirefoxDriver();
List<WebDriver> elements = driver.findElements(By.tagName("a"));

But I get zero links. How can I fix this?

I need to get the part from <a href="url">. I need the URL text.


I think found what I was looking for:

List<WebElement> elements = driver.findElements(By.tagName("a"));
for (int i = 0; i < elements.size(); i++) {
   System.out.println(elements.get(i).getAttribute("href"));
}
1
  • Just a suggestion, instead of the for loop use the for..each syntax. for(WebElement element: elements) Commented Nov 17, 2013 at 21:54

3 Answers 3

4

You forgot to call WebDriver#get in order to access some page.

WebDriver driver = FirefoxDriver();   
driver.get("www.google.com");
List<WebElement> elements = driver.findElements(By.tagName("a")); 
Sign up to request clarification or add additional context in comments.

Comments

2

In the code provided no website is retrieved. Try accessing a web page and then getting the a elements. Also trying changing from List<WebDriver> to List<WebElement>

   WebDriver driver = FirefoxDriver();
   driver.get("http://www.google.com"); 
   List<WebElement> elements = driver.findElements(By.tagName("a")); 

See this example: http://www.seleniumhq.org/docs/03_webdriver.jsp#introducing-the-selenium-webdriver-api-by-example

The following example works for me:

 public class SeleniumTest {

    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();

        // And now use this to visit Google
        driver.get("http://www.google.com");
        List<WebElement> elements = driver.findElements(By.tagName("a"));

        for (WebElement element : elements) {
            System.out.println(element.getText());
        }
    }
}

4 Comments

I did not, I simply forgot to write this part in this question.
So show us all the relevant code including the page you are testing
@PetrMensik My answer is more detailed. J/K
@usr999 See updates regarding the List<WebElement>
0

I was facing same issue but now it resolved If you want to fetch all the links from the webpage you need to use List class and than use webelement

     List<WebElement> allLinks = driver.findElements(By.tagName("a"));
                      int linkcount=allLinks.size();
                      System.out.println("All Links :" +linkcount);
                 
                      for(int j=0 ;j<linkcount;j++)
                       {
                        String ele= driver.findElements(By.tagName("a")).get(j).getText();
                         
                        System.out.println(ele);
                    
                              
                       }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.