53

Selenium WebDriver如何在java中使用自动化拖放功能?

4

13 回答 13

55

有一个页面记录了高级用户交互;其中有很多关于如何生成一系列动作的很好的例子, 你可以在这里找到

// 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();
于 2013-01-08T19:32:36.427 回答
37

Selenium 有很好的文档。是您正在寻找的 API 的特定部分的链接。

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

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

(new Actions(driver)).dragAndDrop(element, target).perform();
于 2013-01-08T07:35:37.823 回答
3

拖放可以这样实现...

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;
}

希望有帮助!

于 2015-06-18T07:16:02.173 回答
3

试试这个:

    Actions builder = new Actions(fDriver);
    builder.keyDown(Keys.CONTROL)
        .click(element)
        .dragAndDrop(element, elementDropped)
        .keyUp(Keys.CONTROL);

        Action selected = builder.build();

        selected.perform();
于 2016-01-11T11:38:10.353 回答
3

Selenium 有很多选项可以执行拖放操作。

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

我已经列出了可能的解决方案,请看一下。

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

于 2015-10-31T19:20:39.937 回答
1
    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(); 
于 2014-12-05T11:51:26.933 回答
1

我会在 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();

这应该做!

于 2015-02-11T03:23:40.410 回答
1

另一种方法是draganddrop()像这样使用

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

     (new Actions(driver)).dragAndDrop(element, target).perform();
于 2014-02-27T05:16:38.557 回答
1

尝试实现下面给出的代码

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();
    }
}
于 2014-10-17T10:36:12.650 回答
0

因为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();
于 2014-04-18T07:08:51.493 回答
0

我使用了下面的一段代码。这里的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);
            }
于 2017-07-21T17:48:43.957 回答
0

Selenium 有很好的文档。这是您正在寻找的 API 的特定部分的链接:

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

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

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

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

于 2016-07-19T09:54:57.477 回答
0
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
于 2016-05-05T08:19:19.880 回答