如何使用 Selenium 執行拖放操作?


我們可以使用 Actions 類的幫助在 Selenium 中執行拖放操作。為了執行拖放操作,我們將使用 dragAndDrop (source, target) 方法。最後,使用 build().perform() 執行所有步驟。

示例

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;
public class DrgAndDrp{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = " https://jqueryui.com/draggable/v";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
      driver.switchTo().frame(0);
      WebElement source= driver.findElement(By.id("draggable"));
      WebElement target= driver.findElement(By.id("droppable"));
      // dragAndDrop() method for dragging the element from source to //destination
      Actions a = new Actions(driver);
      Thread.sleep(5000);
      a.dragAndDrop(source, target).build().perform();
      Thread.sleep(5000);
      driver.quit();
   }
}

更新於:2020 年 6 月 11 日

3K+ 瀏覽量

開啟你的 職業生涯

完成課程即可獲得認證

開始
廣告