1

I tried everything: click, send, etc.

Below is my code:

WebElement element = ((WrapsDriver) selenium)
  .getWrappedDriver().findElement(By.xpath(LOGOUT_XPATH));
element.sendKeys(Keys.ENTER);

try {
  Thread.sleep(500);
} catch (InterruptedException e) {}

Actions moveTo = new Actions(
  ((WrapsDriver) selenium).getWrappedDriver());

moveTo.moveToElement(element).click().build().perform();

But when I executed it, it works sometimes and does not work at other times.

Help is needed.

1
  • 1
    What exactly are you trying to do? From what I can tell you are pressing "Enter" with the focus on a logout link or button, and then trying to move the mouse to the same element and click it? You also have a try/catch around a sleep statement which won't ever catch anything... Maybe you can add more explanation as to what you are trying to do and what specifically is not working. Have you tried just executing the click function and not using the Actions class?
    – Sam Woods
    Commented Aug 16, 2012 at 17:27

1 Answer 1

1

The above doesn't look like what you intend to do, you are sending an ENTER keystroke to the logout element, waiting for 0.5 seconds (no need to wrap that in a try / catch as the sleep should not fail to execute), then you are moving the mouse to the element and then trying to click it.

Only one of the above should be necessary if this is a normal button or link, and it should be possible by simply calling click().

Please let us know what exception is thrown if you call:

WebElement element = ((WrapsDriver) selenium)
  .getWrappedDriver().findElement(By.xpath(LOGOUT_XPATH));
element.click();

If an ElementNotVisible exception is thrown, or more than one element was found, you may be trying to interact with a hidden element, so make sure this is the only logout element matching your XPath, and that this one is visible - the Firefox plugin 'XPath Checker' is very useful for writing and checking XPaths in real time.

It may be useful to write some debug information to the console if you are still stuck. If element.Displayed is false, WebDriver will not be able to click it, so you know you are trying to click the wrong element if you can see it at the time.

Not the answer you're looking for? Browse other questions tagged or ask your own question.