简体   繁体   English

无法拖放Selenium WebDriver

[英]Not able to drag and drop in selenium webdriver

I am using code below to drag and drop action using webdriver 我正在使用下面的代码使用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 . 根据RNS的回答-> WebElements必须是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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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