【问题标题】:Selenium/Java: Drag and drop operation not working for invisible elementsSelenium/Java:拖放操作不适用于不可见元素
【发布时间】:2017-07-11 15:42:24
【问题描述】:

使用 Selenium w/ Java 绑定和 ChromeDriver 2.3 并安装最新的浏览器

经过足够多的时间试图找到解决方案后,我目前被难住了。我目前正在尝试将一个元素拖放到另一个元素。唯一的问题是我需要移动到的元素目标只有在我移动源元素后才可见。有什么建议吗?

所以这里是我单击并按住然后拖动它的源元素的屏幕截图,显示了我也可以删除的两个选项:

Element that is the source

所以当我按住鼠标并拖动一小部分时,放置目标变得可见,如下图所示:

Targets visible once mouse is dragged with source

这是我尝试过的最新代码 sn-p,我相信它应该会实现这一点,但没有任何反应,并继续进入 Thread.sleep(),该代码仅用于观察目的。这些假设是 WebDriverWait 和 WebDriver 的健康实例:

 driverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='user-info ng-binding'][text()='Sample Text']")));           
 driverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='user-info ng-binding'][text()='Sample Text']"))); 
 source = driver.findElement(By.xpath("//div[@class='user-info ng-binding'][text()='Sample Text']"));

 //move to element, click and hold, and then move it to expose the available options
 actions.moveToElement(source).clickAndHold().pause(Duration.ofSeconds(1)).moveByOffset(10, 10).pause(Duration.ofSeconds(1)).build().perform();
 Thread.sleep(10000);

所以这背后的想法是只需单击并按住源,将其移动一点以显示目标,然后找到目标,并使用 actions.release() 将目标移动到源上,但是当我调试它,它对元素没有任何作用,直接进入 Thread.sleep()。我读过很多错误。任何建议将不胜感激。

【问题讨论】:

标签: java selenium drag-and-drop


【解决方案1】:

我知道这是一个老问题,但我一直在寻找解决方案,但最后我只是让隐藏区域可见。我知道这并不理想,但似乎可行。

JavascriptExecutor js = (JavascriptExecutor) driver; 
js.executeScript("arguments[0].setAttribute('style', 'display:block')", targetElement);

【讨论】:

  • 这会设置一个异常,因为驱动程序无法找到该元素?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多