Ram Pasala
Ram Pasala

Reputation: 5231

How can i click on a nested anchor href tag using selenium webdriver?

This is my html code:

<a href="guestProfile.do?reloadGuest=true&amp;gmp=42017073774&amp;resConf=400007053&amp;roomSeq=1&amp;rrsId=1&amp;propId=712" target="sgr" <a="" name="Y">YIKKU, TFYTUR</a>

I want to click on the link name YIKKU TFYTUR, i have tried the following but nothing worked-

driver.findElement(By.partialLinkText("YIKKU, TFYTUR")).click();
driver.findElement(By.cssSelector("a[href*='Y']")).click();

can anyone please help me??

Upvotes: 0

Views: 3007

Answers (2)

Ram Pasala
Ram Pasala

Reputation: 5231

The only solution to these kind of Href tags are find the nearest "id" element, in my case was this-

<table id="resSearchResultsTBL">

then find this element using css selector:

WebElement guest = driver.findElement(By.cssSelector("table[id='resSearchResultsTBL']"));

and then find again in this element a sub element of "a href" tag:

guest.findElement(By.cssSelector("a[href*='guestProfile.do']")).click();

This worked perfectly for me.:)

Upvotes: 2

LittlePanda
LittlePanda

Reputation: 2507

Try -

WebElement link = driver.findElement(By.xpath("//a[@name=\"Y\"]"));
wait.until(ExpectedConditions.elementToBeClickable(link));
link.click();

or

WebElement link = driver.findElement(By.xpath("//a[@target=\"sgr\"]"));
wait.until(ExpectedConditions.elementToBeClickable(link));
link.click();

Upvotes: 1

Related Questions