Joe
Joe

Reputation: 2633

How to get a link href using selenium?

this is a link on a page im automating:

enter image description here

and this is when I inspect it's element:

enter image description here

I want to get only the

/console/agent_invoices/2297294

which is the href...

How can I do this?

thanks!

Upvotes: 1

Views: 3584

Answers (2)

yojna
yojna

Reputation: 513

First find element and get its attribute value in variable as explained below.

String linkText = webDriver.findElement(By.tagName("a")).getAttribute("href");

Upvotes: 2

drkthng
drkthng

Reputation: 6909

You can use the getAttribute method:

String hrefText = yourLinkElement.getAttribute("href");

If you also need to find your linkElement first, you need to provide more code.

Given you only have one link Element:

WebElement yourLinkElement = driver.findElement(By.tagName("a));

Or for example if you already identified the div-Element, that we see with the closing tag in your question you can do this:

WebElement yourLinkElement = divElement.findElement(By.tagName("a));

Upvotes: 3

Related Questions