Xpath for href element
Asked Answered
K

8

20

I need to click on the below href element,which is present among similar href elements.

<a id="oldcontent" href="listDetails.do?camp=1865"><u>Re-Call</u></a>

Can anyone provide me xpath to click the above href link?

Killebrew answered 29/10, 2012 at 15:44 Comment(2)
selenium.click("xpath=//a[@href=\"listDetails.do?camp=1865\"]"); does not workKillebrew
selenium.click("xpath=//a[@href='listDetails.do?camp=1865']"); does not workKillebrew
F
30

Try below locator.

selenium.click("css=a[href*='listDetails.do'][id='oldcontent']");

or

selenium.click("xpath=//a[contains(@href,'listDetails.do') and @id='oldcontent']");
Fishy answered 29/10, 2012 at 18:3 Comment(0)
L
7

This works properly try this code-

selenium.click("xpath=//a[contains(@href,'listDetails.do') and @id='oldcontent']");
Lilley answered 25/4, 2017 at 11:29 Comment(0)
T
3

This will get you the generic link:

selenium.FindElement(By.XPath("xpath=//a[contains(@href,'listDetails.do')")).Click();

If you want to have it specify a parameter then you will have to test for each one:

...
int i = 1;

selenium.FindElement(By.XPath("xpath=//a[contains(@href,'listDetails.do?camp=" + i.ToString() + "')")).Click();
...

The above could utilize a for loop which navigates to and from each camp numbers' page, which could verify a static list of camps.

Please excuse if the code is not perfect, I have not tested myself.

Thomsen answered 16/3, 2020 at 16:7 Comment(0)
D
2

what worked for me:

//a[contains(@href,'logout')]
Drawbridge answered 11/4, 2022 at 13:12 Comment(0)
S
1

have you tried:

//a[@id='oldcontent']/u[text()='Re-Call']
Sinusoid answered 20/12, 2012 at 19:19 Comment(0)
M
1

for me worked //a[text()='Re-Call']

Maines answered 27/4, 2021 at 8:40 Comment(0)
B
0

Below works fine.

//a[@id='oldcontent']

If you've tried certain ones and they haven't worked, then let us know, otherwise something simple like this should work.

Brace answered 29/10, 2012 at 15:52 Comment(9)
There are multiple tags with same id,so am not using that.i want to pick it up using href value itselfKillebrew
selenium.click("css=a[href='listDetails.do?camp=236767']"); also does not work :-(Killebrew
OK, what about //a[@href='listDetails.do?camp=1865'], note that it looks like the integer at the end is a unique ID, so you will need to cater for this. Do you get the element returned if you do a blanket contains search? //a[contains(@href, 'listDetails.do')]Brace
Yes i need to pick up based on the unique id at the end of the listDetails.do?camp=1865,since all hrefs have sommon string listDetails.do?camp..Only integer differsKillebrew
selenium.click("xpath=//a[@href='listDetails.do?camp=1865']"); does not work..Can anyone suggest solutionKillebrew
What browser do you use? Works fine using Chrome v22. Does it find the element at all or just not click on it?Brace
Am using IE8.may be its not finding the elementKillebrew
Have you set up IE8 according to: code.google.com/p/selenium/wiki/InternetExplorerDriver (the 'Required Configuration' section)Brace
FYI if there are multiple elements with the same ID then you have a bigger issue as you have invalid elements. If you have any JavaScript code on the page calling document.getElementById(id); you will get inconsistent results depending on the browser/version. I would HIGHLY recommend fixing the invalid DOM first.Stoner
D
0

Best way to locate anchor elements is to use link=Re-Call:

selenium.click("link=Re-Call");

It will work..

Distaff answered 16/11, 2012 at 8:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.