如何在硒网络驱动程序中将鼠标悬停在图像上以获取菜单列表

How to do Mouse hover on Image in selenium web driver to get menu list

我正在尝试将鼠标悬停在图像上以显示菜单列表。
我的 HTML 代码是:

<img id="logo" src="/web/images/header/img_Logo_Topbar.png">

但我正在尝试将 Xpath 作为 "//*[@id='logo']"。没有回应。
我正在使用这个脚本:

Actions a1 = new Actions(driver);
a1.moveToElement(driver.findElement(By.xpath("//*[@id='logo']")))
                 .build()
                 .perform();
Thread.sleep(1000L);

它看起来像一个错误,我不确定解决这个问题的方法是什么,但如果你想要替代解决方案来在元素上执行鼠标悬停,你可以使用 JavascriptExecutor 如下:-

WebElement element = driver.findElement(By.id("logo"));

((JavascriptExecutor)driver).executeScript("var mouseEvent = document.createEvent('MouseEvents');mouseEvent.initEvent('mouseover', true, true); arguments[0].dispatchEvent(mouseEvent);", element);