How to mouseover a WebElement using Selenium RC2 in Firefox

流过昼夜 提交于 2020-01-13 14:04:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!