问题
Using Selenium's Firefox WebDriver 2.20, I need to display a tooltip that appears when the mouse hovers over a link on my web page.
I've tried using Selenium's Action class to do this, but I get a ClassCastException: $Proxy7 incompatible with org.openqa.selenium.internal.Locatable. Here is what I've tried so far:
Actions builder = new Actions(driver);
WebElement link = driver.findElement(By.tagName("a"));
builder.moveToElement(link).build().perform();
The ClassCastException happens in the moveToElement() method, when the WebElement that I passed to the function is cast to a Locatable object. The method is:
public Actions moveToElement(WebElement toElement) {
action.addAction(new MoveMouseAction(mouse, (Locatable) toElement));
return this;
}
I've also tried the code below, which resulted in the same error:
WebElement link = driver.findElement(By.tagName("a"));
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseDown(((Locatable)link).getCoordinates());
I've heard that these methods worked in previous Firefox versions but not with recent Firefox versions (I'm using FF12). If that is true, are there any other ways of simulating a mouseover in Selenium? Any help getting this function to work would be greatly appreciated!
SOLUTION After digging around for a while and trying different code snippets, I found a solution to the problem. For anyone who has this problem in the future, I had to disable native events for the Firefox driver, like so:
DesiredCapabilities cap = DesiredCapabilities.firefox();
FirefoxProfile prof = new FirefoxProfile();
prof.setEnableNativeEvents(false);
cap.setCapability("firefox_profile", prof);
回答1:
You can use Javascript to do that:
string script = "var evt = document.createEvent('MouseEvents');" +
"evt.initMouseEvent('mouseover',true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" +
"arguments[0].dispatchEvent(evt);";
((IJavaScriptExecutor)driver).ExecuteScript(script, element);
来源:https://stackoverflow.com/questions/11023728/how-to-mouseover-a-webelement-using-selenium-rc2-in-firefox