5

I am having a problem while trying to click on a hyperlink using Selenium Webdriver. I tried using CssSelector as well as XPath and nothing seem to work. All I am trying to do is Click on the Google text.

 <div class="hello">
 <div class="test">
 <table class="first first-table">
 <tbody>
 <tr>
 <td class="second-table">
 <a id="dummyID" href="https://www.google.com" target="_blank">Google</a>
 </td>
 </tr>
 </tbody>
 </table>`

I tried driver.FindElement(By.XPath("//*[@id='dummyID']")).Click(); I also tried using href to find the link...pretty much everything. All the time I am getting "unable to locate element" exception. Anyone has any suggestions?

asked Sep 27, 2013 at 22:03
6
  • 3
    Is the Link contained within a frame or iframe? Commented Sep 27, 2013 at 22:07
  • @Richard That's a good question.. Commented Sep 27, 2013 at 22:07
  • Looking at the HTML using Firebug, I don't see any frame/iframe. Commented Sep 27, 2013 at 22:09
  • I think at this point, we're going to need to see some more html, and possibly your code from initializing the driver to where you attempt to click the link. Commented Sep 27, 2013 at 22:15
  • Try this if it helps stackoverflow.com/questions/13040719/… Commented Sep 27, 2013 at 23:13

4 Answers 4

6

Try this:

driver.FindElement(By.LinkText("Google")).Click();

Look to see if the element is within a frame or iframe. If it is, you'll need to use:

driver.SwitchTo().Frame("frameID");
answered Sep 27, 2013 at 22:05

4 Comments

Then what's the problem here driver.FindElement(By.XPath("//*[@id='dummyID']")).Click() ?
It doesn't have frame..at least the pagesource doesn't show any frame.
@user1493537 afaik, frames may be not present in page source. In your Firebug, on the left side of locator input there's a drop down. What does it show?
Yes, I checked it and there are no frames.
2

You can use

driver.FindElement(By.LinkText("Google")).Click();
gsamaras
73.6k50 gold badges210 silver badges330 bronze badges
answered May 4, 2015 at 8:49

Comments

1

You can easily find by id:

driver.FindElement(By.id("dummyID")).Click();

Or you can use this xpath in your scenario:

driver.FindElement(By.XPath("//a[@id='dummyID']")).Click();

You can directly find the element by id, it is an easy and efficient approach. I hope you will get your desired click on it with any of these code fragments, both should work the same. Surely it will work.

answered May 22, 2014 at 10:02

Comments

0

None of the proposed solutions will work every time. Hyperlinks cannot be treated like other clickable web elements such as buttons. A hyperlink is disabled by removing the href attribute from the anchor tag. For other components, the web driver can interrogate the found web element to see if is enabled or clickable. Consider the following scenario:

<a id="dummyID" target="_blank">Google</a>
WebElement element = new WebDriverWait(driver, 1).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='Google']")));
element.click();

Because the ExpectedConditions class cannot make a determination of the "clickability" of the link like it can do with an input element (such as button), it will not throw a TimeoutException and will allow your test to click a dead (disabled) link. Therefore, for hyperlinks exclusively, you must either search for an anchor tag with an href attribute in it, or test the consequence of the click (i.e. a change in URL).

Another thing you should try to avoid is the use of //* to locate web elements. You can run into situations where more than one element will be found and you'll end up interacting with the incorrect one.

Lastly, the problem with driver.FindElement(...) is that you can run into issues with NoSuchElement exceptions if the page has not loaded fully (unless you set an implicit wait of some kind). The preferred way to wait for an element to interact with, it by using explicit waits, like the Java example I provided above. Since I do not know what issue you ran into, I have to assume this could be your issue. If this is not the issue, perhaps the element is inside an iframe in which case, you will need to tell the web driver to switch to the correct iframe prior to interacting with the element.

answered Jun 4, 2019 at 20:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.