如何使用Selenium WebDriver Java自动拖放function

如何使用Java中的Selenium WebDriver自动拖放function?

有一个logging高级用户交互的页面; 关于如何产生一系列动作有很多很好的例子, 你可以在这里find它

 // Configure the action Actions builder = new Actions(driver); builder.keyDown(Keys.CONTROL) .click(someElement) .click(someOtherElement) .keyUp(Keys.CONTROL); // Then get the action: Action selectMultiple = builder.build(); // And execute it: selectMultiple.perform(); 

要么

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

selenium有相当不错的文件。 这里是你正在寻找的API的特定部分的链接。

 WebElement element = driver.findElement(By.name("source")); WebElement target = driver.findElement(By.name("target")); (new Actions(driver)).dragAndDrop(element, target).perform(); 

selenium有这么多的选项来执行拖放。

在Action类中,我们有几个将执行相同任务的方法。

我列出了可能的解决scheme,请看看。

http://learn-automation.com/drag-and-drop-in-selenium-webdriver-using-actions-class/

拖放可以像这样实现…

 public ObjectPage filter(int lowerThreshold, int highThreshold) { Actions action = new Actions(getWebDriver()); action.dragAndDropBy(findElement(".className .thumbMin"), lowerThreshold, 0).perform(); waitFor(elementIsNotDisplayed("#waiting_dialog")); action.dragAndDropBy(findElement(".className .thumbMax"), highThreshold, 0).perform(); waitFor(elementIsNotDisplayed("#waiting_dialog")); return this; } 

希望有所帮助!

试试这个:

  Actions builder = new Actions(fDriver); builder.keyDown(Keys.CONTROL) .click(element) .dragAndDrop(element, elementDropped) .keyUp(Keys.CONTROL); Action selected = builder.build(); selected.perform(); 
  WebElement fromElement= driver.findElement(By.xpath("SourceElement")); WebElement toElement=driver.findElement(By.xpath("TragetElement")); Actions action = new Actions(WebDriver); Action dragDrop = action.dragAndDrop(fromElement, toElement).build(); dragDrop.perform(); 

我会这样做在Perl中使用Selenium :: Remote :: Driver。

 my $sel = <>; #selenium handle my $from_loc = <fromloc>; my $to_loc = <toloc>; my $from_element = $sel->find_element($from_loc); my $to_element = $sel->find_element($to_loc); # Move mouse to from element, drag and drop $sel->mouse_move_to_location(element=>$from_element); $sel->button_down(); # Holds the mouse button on the element $sel->mouse_move_to_location(element=>$to); # Move mouse to the destination $sel->button_up(); 

这应该做到这一点!

还有一种方法是使用draganddrop()这样的

  WebElement element = driver.findElement(By.name("source")); WebElement target = driver.findElement(By.name("target")); (new Actions(driver)).dragAndDrop(element, target).perform(); 

尝试执行下面给出的代码

 package com.kagrana; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Action; import org.openqa.selenium.interactions.Actions; public class DragAndDrop { @Test public void test() throws InterruptedException{ WebDriver driver = new FirefoxDriver(); driver.get("http://dhtmlx.com/docs/products/dhtmlxTree/"); Thread.sleep(5000); driver.findElement(By.cssSelector("#treebox1 > div > table > tbody > tr:nth-child(2) > td:nth-child(2) > table > tbody > tr:nth-child(2) > td:nth-child(2) > table > tbody > tr:nth-child(3) > td:nth-child(2) > table > tbody > tr > td.standartTreeRow > span")).click(); WebElement elementToMove = driver.findElement(By.cssSelector("#treebox1 > div > table > tbody > tr:nth-child(2) > td:nth-child(2) > table > tbody > tr:nth-child(2) > td:nth-child(2) > table > tbody > tr:nth-child(3) > td:nth-child(2) > table > tbody > tr > td.standartTreeRow > span")); WebElement moveToElement = driver.findElement(By.cssSelector("#treebox1 > div > table > tbody > tr:nth-child(2) > td:nth-child(2) > table > tbody > tr:nth-child(2) > td:nth-child(2) > table > tbody > tr:nth-child(2) > td:nth-child(2) > table > tbody > tr:nth-child(1) > td.standartTreeRow > span")); Actions dragAndDrop = new Actions(driver); Action action = dragAndDrop.dragAndDrop(elementToMove, moveToElement).build(); action.perform(); } } 

对于xpath你可以使用上面这样的命令:

 WebElement element = driver.findElement(By.xpath("enter xpath of source element here")); WebElement target = driver.findElement(By.xpath("enter xpath of target here")); (new Actions(driver)).dragAndDrop(element, target).perform(); 
 import com.thoughtworks.selenium.Selenium; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.WebDriver; import org.openqa.selenium.By; //------------------------------------------------------------------------- import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.interactions.Action; /* Move only @param o WebElement to move @param d WebElement destination element */ m.drag={o,d-> def lo=o.location; def ld=d.location; int di=ld.y - lo.y; int inc,lim if (di<0) { inc=-1 lim=ld.y+d.size.height } else { inc=1 lim=ld.y } def fes={ int act=o.location.y; println "act=${act} ${lim}"; if (inc > 0) return !(act>lim) else return !(act<lim) } def b =new Actions(driver); b.clickAndHold(o).perform(); while ( fes() ){ b.moveByOffset(0,inc);b.perform();sleep(20); } // b.release(m.ori).perform(); }//drag 

selenium有相当不错的文件。 以下是您正在查找的API的特定部分的链接:

WebElement element = driver.findElement(By.name("source"));

WebElement target = driver.findElement(By.name("target"));

(new Actions(driver)).dragAndDrop(element, target).perform();

这是拖放一个文件,如何拖放多个文件。

我使用下面的一段代码。 这里dragAndDrop(x,y)是Action类的一个方法。 其中分别采用两个参数(x,y),源位置和目标位置

 try { System.out.println("Drag and Drom started :"); Thread.sleep(12000); Actions actions = new Actions(webdriver); WebElement srcElement = webdriver.findElement(By.xpath("source Xpath")); WebElement targetElement = webdriver.findElement(By.xpath("Target Xpath")); actions.dragAndDrop(srcElement, targetElement); actions.build().perform(); System.out.println("Drag and Drom complated :"); } catch (Exception e) { System.out.println(e.getMessage()); resultDetails.setFlag(true); }