Reputation: 37
I want to get URL's of hyperlinks present on current page using Selenium Web driver. Can anyone help.
Upvotes: 1
Views: 14049
Reputation: 317
To get the URL of all the links on a page, you can store all the elements with tagname 'a' in a WebElement list and then you can fetch the href attribute to get the link of each WebElement.
you can refer to the following code :
List<WebElement> links = driver.findElements(By.tagName("a")); //This will store all the link WebElements into a list
for(WebElement ele: links) // This way you can take the Url of each link
{
String url = ele.getAttribute("href"); //To get the link you can use getAttribute() method with "href" as an argument
System.out.println(url);
}
Upvotes: 5
Reputation: 1818
Just get them from the href attribute using getAttribute()
(assuming you are in java):
WebElement link = driver.findElement(By.tagName("a"))
String url = link.getAttribute("href")
Upvotes: 0