【问题标题】:How to mouseover a WebElement using Selenium RC2 in Firefox如何在 Firefox 中使用 Selenium RC2 将鼠标悬停在 WebElement 上
【发布时间】:2012-06-16 22:23:41
【问题描述】:

使用 Selenium 的 Firefox WebDriver 2.20,当鼠标悬停在我网页上的链接上时,我需要显示一个工具提示。

我尝试使用 Selenium 的 Action 类来执行此操作,但我得到了 ClassCastException: $Proxy7 incompatible with org.openqa.selenium.internal.Locatable。到目前为止,这是我尝试过的:

Actions builder = new Actions(driver);
WebElement link = driver.findElement(By.tagName("a"));
builder.moveToElement(link).build().perform();

ClassCastException 发生在 moveToElement() 方法中,当我传递给函数的 WebElement 被强制转换为可定位对象时。方法是:

public Actions moveToElement(WebElement toElement) { 
   action.addAction(new MoveMouseAction(mouse, (Locatable) toElement)); 
   return this;
}

我也试过下面的代码,结果还是一样:

WebElement link = driver.findElement(By.tagName("a"));
Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseDown(((Locatable)link).getCoordinates());

我听说这些方法在以前的 Firefox 版本中有效,但不适用于最近的 Firefox 版本(我使用的是 FF12)。如果这是真的,还有其他方法可以在 Selenium 中模拟鼠标悬停吗?任何帮助使此功能正常工作将不胜感激!

解决方案 在挖掘了一段时间并尝试了不同的代码 sn-ps 之后,我找到了解决问题的方法。对于将来遇到此问题的任何人,我必须禁用 Firefox 驱动程序的本机事件,如下所示:

DesiredCapabilities cap = DesiredCapabilities.firefox();

FirefoxProfile prof = new FirefoxProfile(); 
prof.setEnableNativeEvents(false); 
cap.setCapability("firefox_profile", prof);

【问题讨论】:

  • 您的意思似乎是 Selenium WebDriver,没有“Selenium RC2”之类的东西吗?
  • 在您的第一个示例中,第 3 行,您调用 actions,但您的 Actions 变量名为 builder。这只是一个复制粘贴错误吗?否则,代码应该可以工作。您能否发布您的确切 Selenium 版本和堆​​栈跟踪的有趣部分?
  • 在创建 firefox 驱动程序对象时尝试切换 firefox 配置文件的本机事件值。
  • 我在 Firefox 12 中使用 Selenium WebDriver 2.20(以前称为 RC2)。上面代码中的 actions 变量只是一个复制粘贴错误。我会尝试获取更详细的堆栈跟踪。
  • 我更好地指出了这个问题。 ClassCastException 发生在 moveToElement() 方法中,当我传递给函数的 WebElement 被强制转换为可定位对象时。方法是:public Actions moveToElement(WebElement toElement) { action.addAction(new MoveMouseAction(mouse, (Locatable) toElement)); return this; }

标签: selenium selenium-rc mouseover


【解决方案1】:

您可以使用 Javascript 来做到这一点:

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);

【讨论】:

    猜你喜欢
    • 2011-09-08
    • 2011-11-08
    • 1970-01-01
    • 2021-07-15
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    相关资源
    最近更新 更多