pradeep
pradeep

Reputation: 37

How to get the URL of the hyperlink in Selenium driver?

I want to get URL's of hyperlinks present on current page using Selenium Web driver. Can anyone help.

Upvotes: 1

Views: 14049

Answers (2)

Ravikar Bhardwaj
Ravikar Bhardwaj

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

Mo H.
Mo H.

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

Related Questions