简体   繁体   中英

How many ways can we implement Drag & Drop functionality using Selenium and Java

How many ways can we implement Drag & Drop functionality using Selenium and Java?

Code attempt:

Thread.sleep(3000);
WebElement FROM = driver.findElement(By.xpath("(//div[@class='item-container flex-container-horizontal'])[1]"));
Thread.sleep(3000);
WebElement to = driver.findElement(By.xpath("//div[text()='Product Quality?']"));
Thread.sleep(3000);
Actions act=new Actions(driver);
act.dragAndDrop(FROM, to).build().perform();

Drag and Drop functionality can be implemented in multiple ways as follows:

  • Using dragAndDrop() :

     System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); WebDriver driver=new FirefoxDriver(); driver.get("http://jqueryui.com/droppable/"); driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']"))); WebElement from = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("draggable"))); WebElement to = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("droppable"))); new Actions(driver).dragAndDrop(from, to).build().perform();
  • Chaining clickAndHold() and moveToElement() :

     System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); WebDriver driver=new FirefoxDriver(); driver.get("http://jqueryui.com/droppable/"); driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']"))); WebElement drag = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("draggable"))); WebElement drop = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("droppable"))); new Actions(driver).clickAndHold(from).moveToElement(to).release(from).build().perform();
  • Video demonstration:

拖放

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM