Can I get the location of the link through selenium?

I have a link on a webpage. I want to right-click and copy the location of the link, is this possible through selenium 1? For example, I have a web page open and it has a “add a book” link, and it is manually, if I right-click and make a copy of the 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 learn how to copy a link by providing XPath text: “add book”? Or using javascript?

Thanks in advance.

+7
source share
3 answers

Use this code to get the link (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); 
+6
source

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> 
+1
source

Selenium interface has

 getHtmlSource() 

-method. This returns a string in which you can apply the Xpath as follows:

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

All Articles