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

佐手、 提交于 2021-02-10 03:07:24

问题


I am trying to mouse hover on image to display menu list.
My HTML code is:

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

But I am trying Xpath as "//*[@id='logo']". There is no response.
I am using this script:

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

回答1:


It's looks like a bug, I'm not sure what is the solution for this but if you want alternate solution to perform mouse hover on element you can use JavascriptExecutor to as below :-

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


来源:https://stackoverflow.com/questions/39168614/how-to-do-mouse-hover-on-image-in-selenium-web-driver-to-get-menu-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!