java_enthu
java_enthu

Reputation: 2337

Can I get link location through selenium?

I have a link on the webpage. I want to right click and copy the link location is it possible through selenium 1? For example I have a webpage opened and it has a link "add book" and it manually if i right click and do copy link location then it points to http://webserver/webapps/books/addbook.jsp?book_id=44_1&type=reference&promo=none

Is there a way to find out to copy the link by giving an XPath of the text :"add book" ? Or using javascript?

Thanks in advance.

Upvotes: 7

Views: 19648

Answers (3)

Surya
Surya

Reputation: 4536

Am using bellow code to get link location (With Selenium-WebDriver and java binding) :

WebElement link = driver.findElement(By.linkText("add book"));
String linkLocatin = link.getAttribute("href");
System.out.println("Link Location "+linkLocatin);

Upvotes: 7

Michael Blank
Michael Blank

Reputation: 344

In Selenese I use something like this:

<tr>
    <td>storeAttribute</td>
    <td>xpath=//a[text()="add book"]@href</td>
    <td>linkToBook</td>
</tr>
<tr>
    <td>echo</td>
    <td>${linkToBook}</td>
    <td></td>
</tr>

Upvotes: 1

martin
martin

Reputation: 2194

The Selenium-interface has a

getHtmlSource()

-method. This returns a String, on which you can apply an Xpath like this:

//a[text()="add book"]

Upvotes: 0

Related Questions