Matt R
Matt R

Reputation: 31

Unable to find Element by XPath when Link has Javascript

I am trying to retrieve a hyperlink element in Selenium in which the link is actually executing javascript. I am unable to retrieve it despite trying several different methods.
< < NOTE: I am able to retrieve other controls - just struggling with this one > > The element looks like the following; trying to retrieve the test1_decode one:

<section id="specialsection" role="tabpanel" aria-hidden="true">
<ul class="col4">
    <li  class="header2"><a href="javascript:SetApplication('test1_decode');">Example Application</a></li>
    <li  class="header2"><a href="javascript:SetApplication('test2_decode');">Example Application POC</a></li>
</ul>
</section>

I've tried the following: FindElement using XPath contains link text, FindElement using Xpath contains text, FindElement by PartialLinkText, FindElement by LinkText - all fail.

I even tried getting all < a > in a list; I wonder if maybe it's because it has a 0 height?

//These are the different things I've tried:
IWebElement ExAppLogin = driver.FindElement(By.XPath("//a[contains(text(), 'Example Application')]"));

IWebElement ExAppLogin = driver.FindElement(By.XPath("//a[contains(@href, 'test1_decode')]"));

IWebElement ExAppLogin = driver.FindElement(By.PartialLinkText("test1_decode"));

IWebElement ExAppLogin = driver.FindElement(By.LinkText("test1_decode"));

IWebElement ExAppLogin = driver.FindElement(By.PartialLinkText("test1_decode"));

They all generate an exception "Unable to find Element with xpath =="...

Upvotes: 2

Views: 804

Answers (3)

Matt R
Matt R

Reputation: 31

Possible Solution
So there was/were Iframes - thanks to everyone who commented/responded.
looking at each of these in debug at a breakpoint:

driver.FindElements(By.CssSelector("iframe"));
driver.FindElements(By.TagName("iframe"));
driver.WindowHandles;

I determined that there were iframes. Since this is a POC, I just hard-coded to retrieve the iframe I wanted:

driver.SwitchTo().Frame(2);

And I was able progress beyond this point. What is odd is that even though I was able to find the element I wanted:

IWebElement ExAppLogin = driver.FindElement(By.XPath("//a[contains(@href, 'test1_decode')]"));

(i.e. I can view the ExAppLogin variable details in debug since it is not NULL/no exception) But if I do this:

ExAppLogin.Click();

nothing happens!

So for the time being I'm able to continue by just executing the javascript:

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("SetApplication('test1_decode');");

Once I do this, I am encountering another issue, but for the purposes of this specific question, I guess I can consider this resolved - thanks everyone!

Upvotes: 1

KunduK
KunduK

Reputation: 33384

Try following XPath.If this not work please check if there any iframe available. And If, then you have to Swith_to iframe first to access the element.

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(text(),'Example Application')][contains(@href,'test1_decode')]"))).click();;

Upvotes: 0

Mate Mrše
Mate Mrše

Reputation: 8414

You could try with the following CSS

driver.findElement(By.cssSelector("a[href*='test1_decode']"));

The *= will match the string inside the href attribute.

Upvotes: 0

Related Questions