12

Possible Duplicate:
Is there a proved mouseOver workaround for FirefoxDriver in Selenium2?

I want to be able to mouse hover a WebElement with the Java Selenium2 API. Is that possible? I am using the current beta 3.

Community
  • 1
  • 1
Alp
  • 29,274
  • 27
  • 120
  • 198

1 Answers1

25

This will help you:

WebElement elems=driver.findElement(By.linkText("Custom Development"));//Menu Item
WebElement elems1=driver.findElement(By.xpath("//li[@id='item-465']/a"));//Menu
Actions builder = new Actions(driver); 
Actions hoverOverRegistrar = builder.moveToElement(elems1);
hoverOverRegistrar.perform();
elems.click();//at last Menu Item Click
  • 1
    Would love to also know how to do this when the anchor element is not visible because it is a JavaScript clicker... – djangofan Dec 18 '12 at 16:02
  • @djangofan please let me know you have tried with LinkText? –  Dec 24 '12 at 17:16
  • I always forget there is a By locator for link text. I will try but since the link doesn't exist until the click occurs, not sure it would work. – djangofan Dec 24 '12 at 19:40
  • @djangofan Hi can you tell the scenario which you are trying to do? –  Dec 24 '12 at 20:33
  • i am just saying that, since your using a physical mouse action, defined by Actions, in that case, since the anchor may not be visible until after you hover, it might be more appropriate for your findElement by xpath to have only the list item by id (without the anchor). It would still work and on some sites, might be more logical. – djangofan Dec 24 '12 at 22:56
  • @djangofan OK that can be more do how your site developed,some times the id is same for all elements,so search for the stable identifier. –  Dec 25 '12 at 16:34
  • still, if all li elements are the same id, then feed them into a list of WebElements and then access by index. – djangofan Dec 25 '12 at 22:28
  • 2
    @pradeek Thanks for the answer, this works. I notice that the actual hover part can be done in one line: `new Actions(driver).moveToElement(elems1).perform();` – Marquee Feb 01 '13 at 15:15