WebDriver拖放在页面上不起作用 [英] WebDriver Drag-and-Drop does not work on the page
问题描述
我有一个非常简单的测试
I have a very simple test
@Test
void dragAndDrop()
{
driver.get("https://www.testandquiz.com/selenium/testing.html");
driver.manage().window().maximize();
WebElement source = driver.findElement(By.id("sourceImage"));
WebElement target = driver.findElement(By.id("targetDiv"));
builder.dragAndDrop(source, target).perform();
}
测试顺利通过,没有任何例外.但是,它不执行拖放操作. 我在Chrome,Firefox和Edge上尝试过. 感谢任何帮助.
The test passes without any exceptions. However, it does not perform drag-and-drop. I tried on Chrome, Firefox, and Edge. Appreciate any help.
推荐答案
Selenium与HTML5交互时拖放存在问题,我尝试在HTML5页面上使用以下Actions类方法,但它们似乎永远无法正常工作
There are issues with Drag and Drop in Selenium interacting with HTML5, I've tried using the following Actions class methods on HTML5 pages but they never seem to work
dragAndDrop() | clickAndHold() | moveToElement() | release()
另一方面,您可以使用 JS脚本
On the other hand you can use a JS script
String filePath = "C:\\Users\\Wilfred\\Desktop\\drag_and_drop_helper.js";
StringBuffer buffer = new StringBuffer();
String line;
BufferedReader br = new BufferedReader(new FileReader(filePath));
while ((line = br.readLine()) != null)
buffer.append(line);
String javaScript = buffer.toString();
javaScript = javaScript + "$('#sourceImage').simulateDragDrop({ dropTarget: '#targetDiv'});";
((JavascriptExecutor) driver).executeScript(javaScript);
在此链接并且我们不得不诉诸机器人类,这是我不建议的.您可以找到更多信息
The same method doesn't work on this link and we had to resort to robot class which I wouldn't suggest. You can find more information here
这篇关于WebDriver拖放在页面上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!