如何使用 Java 在 Selenium WebDriver 中执行鼠标悬停功能?

我想在下拉菜单上做鼠标悬停功能。当我们将鼠标悬停在菜单上时,它会显示新的选项。 我尝试使用 xpath 单击新选项,但无法直接单击菜单。 因此,作为手动方式,我试图悬停在下拉菜单,然后将点击新的选项。

Actions action = new Actions(webdriver);
WebElement we = webdriver.findElement(By.xpath("//html/body/div[13]/ul/li[4]/a"));
action.moveToElement(we).build().perform();
440989 次浏览

实际上不可能执行“鼠标悬停”动作,相反,您需要链接您想要一次完成的所有动作。所以移动到显示其他元素的元素,然后在同一链中,移动到现在显示的元素并单击它。

在使用动作链时,你必须记住“像用户那样做”。

Actions action = new Actions(webdriver);
WebElement we = webdriver.findElement(By.xpath("html/body/div[13]/ul/li[4]/a"));
action.moveToElement(we).moveToElement(webdriver.findElement(By.xpath("/expression-here"))).click().build().perform();

基于 这个博客文章,我使用 Selenium 2 WebDriver 的以下代码触发了鼠标悬停:

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)driver).executeScript(javaScript, webElement);

当试图做以下事情时,这些答案都不起作用:

  1. 将鼠标悬停在菜单项上。
  2. 找到仅在悬停之后可用的隐藏元素。
  3. 单击子菜单项。

如果在 moveToElement 之后插入一个“ Performance”命令,它会移动到元素,并且子菜单项会短暂显示一段时间,但这不是悬停。在找到隐藏元素并导致 ElementNotFoundException 之前,该元素会立即消失。我试了两种方法:

Actions builder = new Actions(driver);
builder.moveToElement(hoverElement).perform();
builder.moveToElement(clickElement).click().perform();

这对我不起作用。以下对我起作用:

Actions builder = new Actions(driver);
builder.moveToElement(hoverElement).perform();
By locator = By.id("clickElementID");
driver.click(locator);

使用 Actions 悬停和标准的 WebDriver 单击,我可以悬停然后单击。

我发现这个问题正在寻找一种方法来为我的 Javascript 测试做同样的事情,使用 Protracor (一个针对 Selenium 的 Javascript 前端)

我对量角器1.2.0和网络驱动2.1的解决方案是:

browser.actions()
.mouseMove(
element(by.css('.material-dialog-container'))
)
.click()
.perform();

这也接受一个偏移量(我使用它来单击元素的左上角:)

browser.actions()
.mouseMove(
element(by.css('.material-dialog-container'))
, -20, -20  // pixel offset from top left
)
.click()
.perform();

使用 Selenium java WebDriver 鼠标悬停的示例程序:

public class Mhover {
public static void main(String[] args){
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.google.com");
WebElement ele = driver.findElement(By.id("gbqfba"));
Actions action = new Actions(driver);
action.moveToElement(ele).build().perform();
}
}

这段代码运行得非常好:

 Actions builder = new Actions(driver);
WebElement element = driver.findElement(By.linkText("Put your text here"));
builder.moveToElement(element).build().perform();

鼠标移过之后,您就可以继续对显示的信息执行您想要的下一个操作

检查这个示例,看看我们如何实现它。

enter image description here

public class HoverableDropdownTest {


private WebDriver driver;
private Actions action;


//Edit: there may have been a typo in the '- >' expression (I don't really want to add this comment but SO insist on ">6 chars edit"...
Consumer < By > hover = (By by) -> {
action.moveToElement(driver.findElement(by))
.perform();
};


@Test
public void hoverTest() {
driver.get("https://www.bootply.com/render/6FC76YQ4Nh");


hover.accept(By.linkText("Dropdown"));
hover.accept(By.linkText("Dropdown Link 5"));
hover.accept(By.linkText("Dropdown Submenu Link 5.4"));
hover.accept(By.linkText("Dropdown Submenu Link 5.4.1"));
}


@BeforeTest
public void setupDriver() {
driver = new FirefoxDriver();
action = new Actions(driver);
}


@AfterTest
public void teardownDriver() {
driver.quit();
}


}

有关详细答案,请参阅此处 -http://www.testautomationguru.com/selenium-webdriver-automating-hoverable-multilevel-dropdowns/

你可以试试:

WebElement getmenu= driver.findElement(By.xpath("//*[@id='ui-id-2']/span[2]")); //xpath the parent


Actions act = new Actions(driver);
act.moveToElement(getmenu).perform();


Thread.sleep(3000);
WebElement clickElement= driver.findElement(By.linkText("Sofa L"));//xpath the child
act.moveToElement(clickElement).click().perform();

如果你的情况下,网络有很多类别,使用第一种方法。对于所需的菜单,只需要第二个方法。

我试过了,效果很正常

action = ActionChains(driver)
element = driver.find_element_by_xpath("XPath_selector")
action.move_to_element(element).perform()

尝试这种可重复使用的方法,

public void MoveThePoiterToElement(By by){
log.info("Moving the cursor to the element");
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(by));
action.build().perform();
log.info("Cursor moved to the element");
}