在WebDriver中是否有针对IE8和Firefox的mouseOver解决方法?

时间:2012-02-15 23:52:54

标签: java selenium webdriver selenium-webdriver

WebDriver的moveToElement在Firefox和IE中不起作用。它在Chrome浏览器中运行良好。

以下是我使用的代码:

Actions builder = new Actions(driver);       
builder.moveToElement(driver.findElement(By.xpath(Loader.OR.getProperty(object))))
                            .moveToElement(driver.findElement(By.xpath(data)))
                            .click();
Action selectMultiple = builder.build();
selectMultiple.perform();

同样适用于Chrome,但不适用于IE或Firefox。

1 个答案:

答案 0 :(得分:0)

您可以使用Javascript,但不幸的是,IE和Firefox / Chrome的脚本不同:

if (_browserName == BrowserName.InternetExplorer)
   {
        script = "var evt = document.createEventObject();" +
                 "arguments[0].fireEvent('onmouseover', evt);";
   }
else
  {
      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)QuestWebDriver.CurrentDriver).ExecuteScript(script, element); 

对于IE,这些selenium原生方法适用于我:

Actions action = new Actions(driver);
action.MoveToElement(element).Perform();