[英]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.