【问题标题】:Unable to perform drag and drop action on an element using Selenium无法使用 Selenium 对元素执行拖放操作
【发布时间】:2018-01-19 12:00:55
【问题描述】:

我正在尝试对元素执行拖放操作,但它没有发生。

这是我正在处理的页面的 sn-p。在这里,我试图将 Tile “Time” 拖放到 Tile “Approvals” 的位置。 Screenshot

这是我正在使用的代码。

代码

String sSource = "//*[@id=\"PTNUI_LAND_REC14$1_row_0"]";
String sTarget = "//*[@id=\"PTNUI_LAND_REC14$1_row_1"]";
WebElement wSource = TestBase.wDriver.findElement(By.xpath(sSource));
WebElement wTarget = TestBase.wDriver.findElement(By.xpath(sTarget));
Actions aActions = new Actions(TestBase.wDriver);
Action aDragAndDrop = aActions.clickAndHold(wSource).moveToElement(wTarget).release(wTarget).build();
aDragAndDrop.perform();

HTML

源元素

    <div class="ps_grid-row nuitile rsz_w1 rsz_h1" id="PTNUI_LAND_REC14$1_row_0" tx="1.0577777777777777" ty="1" gx=".1.0577777777777777." gy=".1.">
    <div class="ps_grid-cell">
    <div id="win0divPTNUI_LAND_REC_GROUPLET$13" class="ps_box-group psc_layout nuilp " tabindex="0" draggable="true" aria-dropeffect="move" aria-grabbed="false" droppable="true">
    <h2 class="ps_groupleth"><span class="ps-label" id="PTNUI_LAND_REC_GROUPLET_LBL$13">Approvals</span></h2>

目标元素

<div class="ps_grid-row nuitile rsz_w1 rsz_h1" id="PTNUI_LAND_REC14$1_row_1" tx="2.057777777777778" ty="1" gx=".2.057777777777778." gy=".1.">
<div class="ps_grid-cell">
<div id="win0divPTNUI_LAND_REC_GROUPLET$14" class="ps_box-group psc_layout nuilp " tabindex="0" draggable="true" aria-dropeffect="move" aria-grabbed="false" droppable="true">
<div id="win0groupletPTNUI_LAND_REC_GROUPLET$14" class="ps_box-grouplet"><img id="PT_PORTAL_CLEAR_DOT$14" class="ps_process" src="/cs/p91h25r2x/cache/PT_PORTAL_CLEAR_DOT_1.gif" alt=""></div>
<h2 class="ps_groupleth"><span class="ps-label" id="PTNUI_LAND_REC_GROUPLET_LBL$14">Time</span></h2></div></div>

如果您需要更多详细信息,请告诉我。

【问题讨论】:

  • 您看到任何错误吗?使用错误堆栈跟踪更新问题。
  • 这里的实现是一个 HTML 5 拖放,据我所知,它仍然不支持通过鼠标向下/向上移动。一种选择是使用脚本注入来模拟 DND:gist.github.com/florentbr/60ef7cb8d9b1ae690cafc82aad52da73
  • @FlorentB。我不确定如何分析它是否是 HTML 5 并且是否可拖放。但我可以清楚地看到这两个元素的draggable="true"droppable="true"。这不会给人留下两个元素都可以拖放的印象吗?
  • @DebanjanB - 我在执行时没有遇到任何异常。问题是什么都没有发生。
  • @FlorentB - 你能告诉我你是如何理解这是一个 html 5 拖放的吗?

标签: java selenium selenium-webdriver automation cucumber


【解决方案1】:

您可能必须将目标定位在可拖动元素上,如下所示。

String sSource = "//*[@id=\"win0divPTNUI_LAND_REC_GROUPLET$13"]";
String sTarget = "//*[@id=\"win0divPTNUI_LAND_REC_GROUPLET$14"]";
WebElement wSource = TestBase.wDriver.findElement(By.xpath(sSource));
WebElement wTarget = TestBase.wDriver.findElement(By.xpath(sTarget));
Actions aActions = new Actions(TestBase.wDriver);
aActions.dragAndDrop(wSource, wTarget).build().perform();

【讨论】:

  • 感谢您的回复。我试过了,但它也不起作用。
【解决方案2】:

对于DragDrop Tile Approvals 在Tile Time 的位置,您可以使用以下代码块:

WebElement  from = TestBase.wDriver.findElement(By.xpath("//div[@class='ps_box-group psc_layout nuilp ' and contains(id,'win0divPTNUI_LAND_REC_GROUPLET$13')]"));
WebElement  to = TestBase.wDriver.findElement(By.xpath("//div[@class='ps_box-group psc_layout nuilp ' and contains(id,'win0divPTNUI_LAND_REC_GROUPLET$14')]"));
new Actions(TestBase.wDriver).dragAndDrop(from, to).build().perform();
System.out.println("Drag and Drop Completed");

【讨论】:

  • 您查看此答案了吗?让我知道状态。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-28
  • 1970-01-01
相关资源
最近更新 更多