简体   繁体   English

MoveToElement不适用于鼠标悬停并按住元素上的单击

[英]MoveToElement doesn't work to mouse hover and hold the click on an element

Following is the sample HTML code of the page: 以下是该页面的示例HTML代码:

在此输入图像描述

I'm trying to mouse hover on the element with following xpath: 我正在尝试使用以下xpath将鼠标悬停在元素上:

WebElement Bar1 = dvr.findElement(By.xpath("//div[@class='barModel']/div[@class='model']/canvas[@class='segment']")));

Following is the code 以下是代码

act = new Action (driver);
act.moveToElement(Bar1).build().perform();
act.clickAndHold();

My objective is to drag the element. 我的目标是拖动元素。 Running the above code doesn't give any error but there is no visible interaction on the page. 运行上面的代码不会给出任何错误,但页面上没有可见的交互。 I am able to do this task using Robot class but just curious to make it happen using Action class. 我能够使用Robot类完成这个任务,但只是好奇使用Action类来实现它。

You didn't performed clickAndHold action: 您没有执行clickAndHold操作:

act = new Action (driver);
act.moveToElement(Bar1).build().perform();
act.clickAndHold().perform();

If you want to drag and drop element you can just use build-in function: 如果要拖放元素,可以使用内置函数:

act.dragAndDrop(movedElement, targetElement).perform();

You can use JavaScriptExecutor to perform mouseover as like below :- 您可以使用JavaScriptExecutor执行mouseover ,如下所示: -

JavascriptExecutor js = (JavascriptExecutor) dvr;
js.executeScript("var clickEvent = document.createEvent('MouseEvents');clickEvent.initEvent('mouseover', true, true); arguments[0].dispatchEvent(clickEvent);", Bar1);

By using above Java Script you can perform mouseover . 通过使用上面的Java Script您可以执行mouseover Now you can use dragAndDrop to move element as below :- 现在您可以使用dragAndDrop移动元素,如下所示: -

Actions action = new Actions(dvr)
action.dragAndDrop(sourceElement, destinationElement).build().perform()

Hope it will help you...:) 希望它会帮助你...... :)

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

相关问题 Selenium WebDriver鼠标操作moveToElement在Firefox Linux上不会引发mouseout事件 - Selenium WebDriver mouse actions moveToElement doesn't raise mouseout event on Firefox Linux Selenium moveToElement两次然后点击元素不起作用 - Selenium moveToElement twice and then click on element is not working 如何鼠标悬停并单击webdriver中的元素 - How to mouse hover and click on element in webdriver 如何使用JRadioButton select鼠标单击似乎不起作用 - How to consume JRadioButton select on mouse click doesn't seem to work Intellij不在鼠标悬停时显示文档 - Intellij doesn't show documentation in mouse hover moveToElement()执行悬停操作 - moveToElement() to perform a hover action 切换到 iframe 并单击元素不起作用并获得 StaleElementReferenceException - Switch to iframe and click on element doesn't work and get StaleElementReferenceException Selenium / Firefox:命令“.click()”不适用于找到的元素 - Selenium / Firefox: Command “.click()” doesn't work with a found element JNA / WinAPI。 模拟鼠标点击而不移动 cursor 不能正常工作 - JNA / WinAPI. Simulate mouse click without moving the cursor doesn't work correctly 鼠标单击侦听器不会触发 - Mouse Click Listener doesn't fire
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM