QualityThoughts
QualityThoughts

Reputation: 79

Fetching all href links from the page source using Selenium WebDriver with Java

I am trying to test HTTP RESPONSE of all href links on the page, using WebDriver to fetch all the links from the page and then using http.connect to get the response status.

Code snippet to get links of anchor tag:

List<WebElement> list = driver.findElements(By.cssSelector("a"));
for (WebElement link : list) {
    System.out.println(link.getText());
}

But my page has many more href links which are not having anchor tag <a> and might reside outside body of the page in header section or so. Some examples are as shown below. Above webdriver code wont solve in fetching all types of links. Also need to extract src links in some cases...

<script src="https://www.test.com/js/50/f59ae5bd.js"></script> 
<link rel="stylesheet" href="www.test.com/css/27/5a92c391c7be2e9.css" rel="stylesheet" type="text/css" />
<link sizes="72x72" href="https://www.test.com/css/27/5a92c391c7b/kj32.png" />
<li><a href="https://www.test.com/webapps/mpp/resortcheck">resortcheck</a>

I would appreciate if someone can guide how to go about or has resolved similar issues in getting all href links from page.

Upvotes: 1

Views: 14155

Answers (2)

vins
vins

Reputation: 15390

You can use Xpath to get all the elements containing the attributes href / src.

List<WebElement> list=driver.findElements(By.xpath("//*[@href or @src]"));

I tried something like this to get all the links to the other resource files. It works fine.

       WebDriver driver = new FirefoxDriver();
       driver.get("http://www.google.com");

       List<WebElement> list=driver.findElements(By.xpath("//*[@href or @src]"));

       for(WebElement e : list){
           String link = e.getAttribute("href");
           if(null==link)
               link=e.getAttribute("src");
           System.out.println(e.getTagName() + "=" + link);
       }

Upvotes: 3

Uday
Uday

Reputation: 1484

What do you mean by links exists outside of body?

All links are identifiable by html tag. What other ways to represent links?

Check my below code may help:

public static void main(String[] args)
{
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.google.com/");
    List<WebElement> links=driver.findElements(By.tagName("a"));
    for(WebElement ele:links)
        System.out.println(ele.getAttribute("href"));
}

Upvotes: 1

Related Questions