Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 324
java_enthu Avatar asked Nov 18 '11 08:11

java_enthu


People also ask

Where can I find link in Selenium?

Using Link Text In Selenium To Locate An Element In order to access link using link text in Selenium, the below-referenced code is used: driver. findElement(By. linkText("this is a link text"));

Is href a locator in Selenium?

We can click a link by href value with Selenium webdriver. To identify the link with the href attribute we can take the help of the locators xpath or css selector. We shall use the findElement method and pass By.


2 Answers

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);
like image 142
Surya Avatar answered Oct 09 '22 10:10

Surya


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>
like image 29
Michael Blank Avatar answered Oct 09 '22 11:10

Michael Blank