【问题标题】:How to automate drag & drop functionality using Selenium WebDriver Java如何使用 Selenium WebDriver Java 自动化拖放功能
【发布时间】:2023-03-03 10:27:02
【问题描述】:

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

【问题讨论】:

  • 请给我们一个可接受的答案
  • 我不明白这个问题怎么从来没有结束过……还有这么多的赞成票???没有研究演示,没有代码示例,......这是给我的代码。

标签: java selenium selenium-webdriver drag-and-drop


【解决方案1】:

有一个页面记录了高级用户交互;其中有很多关于如何生成一系列动作的很好的例子,you can find it here

// 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();

【讨论】:

  • 当你使用.release(otherElement)时,.moveToElement(otherElement)不是不必要的吗?
  • builder.keyDown(Keys.CONTROL) .click(someElement) .click(someOtherElement) .keyUp(Keys.CONTROL); // 然后获取动作: Action selectMultiple = builder.build(); // 并执行它:selectMultiple.perform();此代码用于拖放吗?我认为,这是用于多项选择。
  • 当我尝试运行它时,我得到:- 线程“main”org.openqa.selenium.interactions.MoveTargetOutOfBoundsException 中的异常:将目标移出边界,知道如何解决它
【解决方案2】:

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

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

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

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

【讨论】:

  • 总是喜欢从目标链接添加代码/示例,链接可能会在不久的将来更改/失效,因此,答案可能对未来的用户无效。我现在已经添加了。
  • 为什么(new Actions(driver)) 周围有额外的括号?
  • 对拖放很有帮助!谢谢!
【解决方案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;
}

希望有帮助!

【讨论】:

    【解决方案4】:

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

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

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

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

    【讨论】:

      【解决方案5】:

      试试这个:

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

      【讨论】:

        【解决方案6】:
            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(); 
        

        【讨论】:

          【解决方案7】:

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

          应该这样做!

          【讨论】:

            【解决方案8】:

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

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

            【讨论】:

              【解决方案9】:

              尝试实现下面给出的代码

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

              【讨论】:

                【解决方案10】:

                对于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();
                

                【讨论】:

                  【解决方案11】:
                  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
                  

                  【讨论】:

                    【解决方案12】:

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

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

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

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

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

                    【讨论】:

                      【解决方案13】:

                      我使用了下面的一段代码。 这里 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);
                                  }
                      

                      【讨论】:

                        猜你喜欢
                        • 1970-01-01
                        • 1970-01-01
                        • 2012-08-04
                        • 1970-01-01
                        • 2023-03-31
                        • 2014-07-23
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多