Go to site : google.com and find how many links are there and then print on the console.
generally links are associated with tag "a" so using this we can solve the above problem.
generally links are associated with tag "a" so using this we can solve the above problem.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExtractingLinks { | |
public static void main(String[] args) throws InterruptedException { | |
// TODO Auto-generated method stub | |
WebDriver driver= new FireFoxDriver(); | |
driver.get("http://www.google.com/"); // | |
Thread.sleep(5000); | |
try { | |
// tag a to find the total counts | |
List<WebElement> n = driver.findElements(By.tagName("a")); | |
int noflinks = n.size(); | |
System.out.println(noflinks); | |
// for loop to traverse and print on the screen | |
for (int i=0;i<=noflinks;i++) | |
{ | |
String linktext = n.get(i).getText(); | |
System.out.println(linktext); | |
String hrefname = n.get(i).getAttribute("href"); | |
System.out.println(hrefname); | |
} | |
}catch (Exception e){ | |
System.out.println("error "+e); | |
} | |
} | |
} |
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.