2

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 =="...

10
  • Selenium does seem to have issues with zero height elements. Have you tried removing the height restrictions temporarily to see if that fixes the issue? Commented Mar 25, 2019 at 15:10
  • When you say remove the height restrictions: do you mean there is a way to Find/parse by ignoring height? Or do you mean: modify the original page to force it to have a height on the control? I can't do the latter unfortunately... Commented Mar 25, 2019 at 15:32
  • Welcome to SO. Were you able to find the elements in the browser dev tools elements tab with the locator strategies mentioned above? Commented Mar 25, 2019 at 15:38
  • And check if you have any iframe, which is wrapping this section. Commented Mar 25, 2019 at 15:39
  • @supputuri I was not able to; I am new to Selenium, so assume I know nothing. How can I do this? Commented Mar 25, 2019 at 18:24

3 Answers 3

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

You could try with the following CSS

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

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

1 Comment

Thanks for the suggestion. Unfortunately, I still get an exception: Unable to find element with css selector == a[href*='test1_decode']
0

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();;

5 Comments

There aren't any IFrames; and for some reason it is not finding any of the values I put in XPath. I think it has to do with the fact that it is not visible/height=0?
@MattR Did you try inducing a WebDriverWait as suggested?
I did it in java.I don’t know c#. The logic remain same only syntax would be different.
I did do that; it turns out my issue was related to IFrames. Thank you!
@MattR : I told you but you denied earlier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.