【问题标题】:Selenium Java Drag and Drop with HTML5Selenium Java 拖放与 HTML5
【发布时间】:2020-08-03 15:24:42
【问题描述】:

在这里的几个线程中,发布了一个解决方法,用于使用 HTML5 进行拖放的页面中的硒拖放。此解决方法涉及使用 javascript 来模拟拖放,例如 Unable to perform HTML5 drag and drop using javascript for Selenium WebDriver testhttps://gist.github.com/rcorreia/2362544。此解决方案在此页面上运行良好,http://the-internet.herokuapp.com/drag_and_drop

一般的做法是把这里的javascript文件(https://gist.github.com/rcorreia/2362544#file-drag_and_drop_helper-js)读成一个字符串,下面简称为'jsfile'。

然后在 selenium(使用 java)中,传入源和目标的 css 选择器,其中 #column-a 是源的 id,#column-b 是目标。

 ((JavascriptExecutor) driver).executeScript(jsfile +"$('#column-a').simulateDragDrop({ dropTarget: '#column-b'});");

它在该页面上就像一个冠军。

但是,类似的方法似乎不适用于此页面,https://crossbrowsertesting.github.io/drag-and-drop.html。当我运行时没有任何反应

 ((JavascriptExecutor) driver).executeScript(jsfile +"$('#draggable').simulateDragDrop({ dropTarget: '#droppable'});");

我的页面看起来像第二页(例如没有拖放)。作为理解这一点的第一步,我想知道为什么这种方法在后一种情况下似乎不起作用。

【问题讨论】:

标签: java html selenium automation


【解决方案1】:

在重新测试https://crossbrowsertesting.github.io/drag-and-drop.html 时,看起来直接使用 Actions 类可以解决拖放问题。在我正在测试的特定应用程序中,它设置了一些额外的代码来帮助实现可访问性,我能够通过将焦点设置在第一个元素上并点击返回键,然后将焦点设置在目标元素并再次点击返回。我相当确定这是自定义事件处理,因此可能不适用于其他应用程序。以防万一,我在这里发布了在 selenium 中执行此操作的代码。

 public void dndHtml5(String xPathSource, String xPathDestination) {
    clickEnterKeyOnElement(xPathSource);
    clickEnterKeyOnElement(xPathDestination);
}

public void clickEnterKeyOnElement(String xPath) {
    setFocusOnElement(xPath);
    WebElement target=element(xPath);
    target.sendKeys(Keys.ENTER);
}

public void setFocusOnElement(String xPath) {
    WebElement element = element(xPath);
    Actions actions = new Actions(driver);
    actions.moveToElement(element).build().perform();
}

public WebElement element(String xPath){
    return driver.findElementByXPath(xPath);
}

【讨论】:

  • 你想试试“seleniumeasy.com/test/drag-and-drop-demo.html”吗,我一直在关注关于拖放的问题,我一直在尝试在上述链接中实现这一点,但徒劳无功.我尝试过使用 Action 类、自定义 JS 执行器和所有努力都是徒劳的。我很高兴看到给定链接中的拖放功能
  • 我试过了,但在 seleniumeasy.com/test/drag-and-drop-demo.html 上没有运气。使用 rcorreia 的 JS。我可以从可拖动列表中拖动项目,但它们在已删除项目列表中显示为未定义。正如您所指出的,Actions 类也没有完成这项工作。似乎需要某种其他类型的自定义 JS 拖放方法,但我不太清楚如何处理它。这是一个html5拖放的页面,w3schools.com/html/html5_draganddrop.asp,rcorreia的js效果很好,为可拖动元素传入“div#div1 img”,为目标传入#div2。
  • 其次,Actions 类的拖放操作适用于某些页面,rcorreia 的 JS 脚本也是如此,我真的很惊讶为什么在 seleniumeasy 网站上很难实现这一点,但看起来我们俩都一样遇到了死胡同,我们会等到有人让它工作。感谢您的尝试:)
猜你喜欢
  • 2018-01-21
  • 1970-01-01
  • 1970-01-01
  • 2018-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
  • 1970-01-01
相关资源
最近更新 更多