简体   繁体   English

如何在Selenium 2中使用JavaScript执行器将鼠标悬停?

[英]How to mouse hover using javascript executor in selenium 2?

Currently working in Selenium WebDriver and using Java . 目前在Selenium WebDriver中使用Java I want to perform the action as mouse hover to the tab and it need to click the tab. 我想在鼠标悬停到选项卡上时执行操作,它需要单击选项卡。 Here in this code i can able to identify the element and it is opening the tab. 在此代码中,我可以识别该元素,并且它正在打开选项卡。 But the problem is it is not mouse hovering and not clicking, it is directly opening the page. 但是问题在于它不是鼠标悬停和没有点击,而是直接打开页面。

 JavascriptExecutor executor = (JavascriptExecutor)driver;
         executor.executeScript("arguments[0].click();",                                                     
driver.findElement(By.id("ext-pr-backlog-evolution") ));

If i use the 如果我使用

WebElement menuHoverLink = driver.findElement(By.id("ext-pr-backlog-evolution"));
actions.moveToElement(menuHoverLink).click().perform();

It is not exactly finding the element and it is clicking some other tab. 它不能完全找到元素,而是单击其他标签。 so i want to mouse hover in javascript executor. 所以我想将鼠标悬停在javascript执行器中。

You can create a mouse event 您可以创建一个鼠标事件

document.createEvent('MouseEvents');

then assign a mouse hover to it 然后将鼠标悬停给它

mouseEventObj.initEvent( 'mouseover', true, true );

and then dispatch it 然后分派

element.dispatchEvent(mouseEventObj);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM