5

I'm trying to click a link and am having difficulties. The relevant HTML code is:

<div id="adHocAddDocDiv" style="display: block;">
    <a href="javascript:hideDiv();" style="color:#000">
        Close window
    </a>
    <table border="0">
        <tbody></tbody>
    </table>
</div>

For code, I have:

driver.findElement(By.xpath("//*[@id='adHocAddDocDiv']/a")).click();

This does find the correct element, however it doesn't seem to execute the JavaScript to close the window that happens if I manually click the link. Any ideas?

UPDATE: Here is the code that finally worked:

WebElement element = driver.findElement(By.xpath("//[@id='adHocAddDocDiv']/a"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
GregMa
  • 740
  • 2
  • 10
  • 25
  • Try to use `//a[@href=\"javascript:hideDiv()\`. – olyv Jun 19 '14 at 19:59
  • That looks incomplete. – GregMa Jun 19 '14 at 20:51
  • is there a single hyperlink in this div??? if not then what is the position of this href link – Anuragh27crony Jun 20 '14 at 06:54
  • Of course it is incomplete :) `//a[@href=\"javascript:hideDiv()"\]` – olyv Jun 20 '14 at 07:31
  • That did not do it either. I even tried doing a sendkeys(keys.enter). Same results. I've verified I had the correct element because a gettext returns the correct text. It acts like it's being clicked, but it is not performing the action. It's not executing the javascript:hideDiv(). Is there no way to get Selenium to execute this? – GregMa Jun 20 '14 at 14:34

3 Answers3

4

I frequently come across elements that WebDriver doesn't seem to be able to click. In these cases I use the following pattern:

var js = (IJavaScriptExecutor)driver;
js.ExecuteScript("$j(\"div[id='adHocAddDocDiv']\").click();");

This is the C# version. I'm sure the Java form is quite similar.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
CynicalBiker
  • 579
  • 5
  • 14
  • This was the answer! For some reason, using the webdriver .click() did not work at all. However, switching to the javascript .click() worked perfectly. I have added the exact java code I used to the original post. Thank you so much for pointing me in the right direction! – GregMa Jul 21 '14 at 14:22
  • 1
    How would you do this in Python? – magicsword Oct 21 '18 at 23:44
1

Try more explicit:

driver.findElement(By.linkText("Close window")).click();
SiKing
  • 10,003
  • 10
  • 39
  • 90
  • No change trying that command. It still won't close the window. – GregMa Jun 19 '14 at 19:24
  • Is there frames involved? Are you sure you have the right element? How do you know? – SiKing Jun 19 '14 at 19:26
  • The element is in a frame. I have switched to that frame and it still doesn't close the window. It's not giving an error that it can't find the element, it's actually executing the .click(), it's just seems to not be actually running the hideDiv() javascript function to close the window. – GregMa Jun 19 '14 at 19:41
0

My guess is that there are more <a>'s immediately following that div, and it's not unique enough. Try this:

driver.findElement(By.cssSelector("div#adHocAddDocDiv > a[href*='hideDiv()']")).click()
ddavison
  • 28,221
  • 15
  • 85
  • 110