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?
-
3Is the Link contained within a frame or iframe?Richard– Richard2013年09月27日 22:07:16 +00:00Commented Sep 27, 2013 at 22:07
-
@Richard That's a good question..Arup Rakshit– Arup Rakshit2013年09月27日 22:07:43 +00:00Commented Sep 27, 2013 at 22:07
-
Looking at the HTML using Firebug, I don't see any frame/iframe.user1493537– user14935372013年09月27日 22:09:25 +00:00Commented 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.Richard– Richard2013年09月27日 22:15:40 +00:00Commented Sep 27, 2013 at 22:15
-
Try this if it helps stackoverflow.com/questions/13040719/…user1769790– user17697902013年09月27日 23:13:34 +00:00Commented Sep 27, 2013 at 23:13
4 Answers 4
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");
4 Comments
driver.FindElement(By.XPath("//*[@id='dummyID']")).Click()
?You can use
driver.FindElement(By.LinkText("Google")).Click();
Comments
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.
Comments
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.