我想将鼠标悬停在selenium webdriver中的元素上进行safari。我尝试了以下方法来实现鼠标悬停:
1)使用 Action 类,这是一个高级互动API,但Safari不支持它。
Actions builder = new Actions(webDriver);
builder.moveToElement(webDriver.findElement(By.xpath("my_locator_xpath"))).build().perform();
2)使用以下 JavaScript执行器代码:
JavascriptExecutor js = (JavascriptExecutor) webDriver;
js.executeScript("arguments[0].onmouseover()", my_element_locator);
String javaScript = "var evObj = document.createEvent('MouseEvents');" +"evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" +"arguments[0].dispatchEvent(evObj);";
((JavascriptExecutor)webDriver).executeScript(javaScript, dept_elem);
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement someElem = driver.findElement(By.id("some Id")); //replace with your own WebElement call/code here
js.executeScript(mouseOverScript, someElem);
3)使用机器人类。
Point elem_coords = my_web_element.getLocation();
Robot robot = new Robot();
robot.mouseMove(elem_coords.x, elem_coords.y);
上述所有方法都无法实现鼠标悬停。代码被执行,我甚至没有得到任何错误。
我正在使用的版本配置是:
Mac OS-OS X 10.8
Safari-版本6.0(8536.25)
Selenium standalone jar-2.45