简体   繁体   中英

Not able to drag and drop in selenium webdriver

I am using code below to drag and drop action using webdriver

driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop");
    driver.switchTo().frame("iframeResult");
    Actions act = new Actions(driver);
    act.dragAndDrop(driver.findElement(By.xpath("html/body/img")),driver.findElement(By.xpath("html/body/div[1]"))).perform();

it is dragging the element but not dropping it in the target

Try following solution :

WebElement From = driver.findElement(By.xpath(".//*[@id='drag1']"));   

WebElement To = driver.findElement(By.xpath(".//*[@id='div1']"));   

Actions builder = new Actions(driver); 

Action dragAndDrop = builder.clickAndHold(From).moveToElement(To).release(To).build();   

dragAndDrop.perform();

Hope it will help you.

driver.get("http://jqueryui.com/droppable/");
          driver.switchTo().frame(0);  
          driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

          WebElement dragElement=driver.findElement(By.id("draggable"));
          WebElement dropElement=driver.findElement(By.id("droppable"));
          Actions builder = new Actions(driver);  // Configure the Action
          Action dragAndDrop = builder.clickAndHold(dragElement)
            .moveToElement(dropElement)
            .release(dropElement)
            .build();  // Get the action
            dragAndDrop.perform(); // Execute the Action

Try this code i hope it will help you

According to RNS answer -> the WebElements must be final .

final WebElement From = driver.findElement(By.xpath(".//*[@id='drag1']"));   
final WebElement To = driver.findElement(By.xpath(".//*[@id='div1']"));   

Actions builder = new Actions(driver); 
Action dragAndDrop = builder.clickAndHold(From).moveToElement(To).release(To).build();   
dragAndDrop.perform();

Or using Serenity

final WebElement From = driver.findElement(By.xpath(".//*[@id='drag1']"));   
final WebElement To = driver.findElement(By.xpath(".//*[@id='div1']")); 

withAction().dragAndDrop(From, To).build().perform();

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