如何在Firefox中使用Selenium RC2将鼠标悬停在WebElement上 [英] How to mouseover a WebElement using Selenium RC2 in Firefox
问题描述
使用Selenium的Firefox WebDriver 2.20,我需要显示一个工具提示,当鼠标悬停在我的网页上的链接上时出现.
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.
我尝试使用Selenium的Action类来执行此操作,但是却收到ClassCastException:$ Proxy7与org.openqa.selenium.internal.Locatable不兼容.到目前为止,这是我尝试过的:
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();
当我传递给函数的WebElement强制转换为Locatable对象时,ClassCastException发生在moveToElement()方法中.该方法是:
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());
我听说这些方法在以前的Firefox版本中有效,但不适用于最新的Firefox版本(我使用的是FF12).如果是这样,是否还有其他方法可以模拟Selenium中的鼠标悬停?任何帮助使该功能正常工作的帮助将不胜感激!
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!
解决方案 经过一段时间的研究并尝试了不同的代码段后,我找到了解决该问题的方法.对于以后遇到此问题的任何人,我都必须为Firefox驱动程序禁用本机事件,如下所示:
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);
推荐答案
您可以使用Javascript来做到这一点:
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);
这篇关于如何在Firefox中使用Selenium RC2将鼠标悬停在WebElement上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!