Selenium Webdriver Java -在执行拖放操作时寻找Actions和Robot的替代方案



我第一次尝试了Actions类,拖放确实可以在不同的元素上工作,但是它无法在它的确切屏幕/网页位置上定位特定的可拖放元素。

下面是我使用的代码:
    Point loc = driver.findElement(By.id("thiselement")).getLocation();
    System.out.println(loc);
    WebElement drag = driver.findElement(By.id("thiselement"));
    Actions test = new Actions(driver);
    test.dragAndDropBy(drag, 0, 60).build().perform();

我用它的像素位置检查了元素,它打印(837,-52),这是在网页顶部的某个地方,距离实际元素有像素。

然后我尝试使用Robot类并在我的脚本上完美地工作,但只能在单个测试机器上提供恒定的成功运行,在不同的机器上运行它具有不同的屏幕分辨率和屏幕大小将使脚本失败,因为Robot依赖于元素的像素位置。

我使用的Robot脚本的示例代码:

Robot dragAndDrop = new Robot();

  dragAndDrop.mouseMove(945, 166); //actual pixel location of the draggable element
  dragAndDrop.mousePress(InputEvent.BUTTON1_MASK);
        sleep(3000);
  dragAndDrop.mouseMove(945, 226);
  dragAndDrop.mouseRelease(InputEvent.BUTTON1_MASK);
        sleep(3000);

Actions和Robot是否有其他选择来自动拖放?或者也许是一个帮助工作的脚本工作的动作,因为我真的不能使用机器人。

另一种拖放方式,

Actions test= new Actions(driver);
builder.clickAndHold("thisElement").moveToElement("targetElement")
.release("targetElement")
.build().perform();

这篇文章会对你有帮助

最新更新