【问题标题】:How I can Sort this Draggable List in Selenium我如何在 Selenium 中对这个可拖动列表进行排序
【发布时间】:2021-09-15 05:33:37
【问题描述】:

练习链接 https://demoqa.com/sortable

我用这段代码试过

Actions action=new Actions(driver);
List<WebElement> elements=driver.findElements(By.xpath("//*[@id=\"demo-tabpane-list\"]/div/div"));
for(int i=5;i>=0;i--) {


action.dragAndDrop(elements.get(0), elements.get(i)).build().perform();
}

【问题讨论】:

  • 你想在那里做什么?我看到元素已经在那里排序了
  • 实际上,我正在尝试以相反的顺序对元素进行排序。如果我将手动将元素从一个位置拖动到另一个位置,那么 Order 也不需要更改。 Atlast 我想以相反的顺序对元素进行排序。

标签: java selenium selenium-webdriver ui-automation qa


【解决方案1】:
List<WebElement> list = driver.findElements(By.xpath("//* [@id='demo-tabpane-list']/div/div"));
    
for(int i =1;i<list.size();i++) {
        
WebElement element = driver.findElement(By.xpath("//*[@id='demo-tabpane-list']/div/div["+ i +"]"));
        
        WebElement destination6 = driver.findElement(By.xpath("//*[@id='demo-tabpane-list']/div/div[6]"));
        WebElement destination5 = driver.findElement(By.xpath("//*[@id='demo-tabpane-list']/div/div[5]"));
        WebElement destination4 = driver.findElement(By.xpath("//*[@id='demo-tabpane-list']/div/div[4]"));
        WebElement destination3 = driver.findElement(By.xpath("//*[@id='demo-tabpane-list']/div/div[3]"));
        WebElement destination2 = driver.findElement(By.xpath("//*[@id='demo-tabpane-list']/div/div[2]"));
        WebElement destination1 = driver.findElement(By.xpath("//*[@id='demo-tabpane-list']/div/div[1]"));
        
        Actions action = new Actions(driver);
        
        if(element!=null) {
        action.dragAndDrop(element, destination6).perform();
        action.dragAndDrop(element, destination5).perform();
        action.dragAndDrop(element, destination4).perform();
        action.dragAndDrop(element, destination3).perform();
        action.dragAndDrop(element, destination2).perform();
        action.dragAndDrop(element, destination1).perform();
        break;
        }
    }

Please enhance it as per your need.

【讨论】:

  • 实际上,我正在尝试以相反的顺序对元素进行排序。如果我将手动将元素从一个位置拖动到另一个位置,那么 Order 也不需要更改。 Atlast 我想以相反的顺序对元素进行排序。你能帮帮我吗?
  • 感谢您的回复,您能帮我解决一下形容词吗?
  • 你能检查一下上面的答案吗
  • 感谢您的帮助,但我在行添加调试点,其中定义了操作对象。之后,我用四个拖动一个,用两个拖动三个。所以 Atlast 失败了。
  • 您是否手动拖动该元素?因为代码只是按降序对元素进行排序。
猜你喜欢
  • 1970-01-01
  • 2013-08-18
  • 1970-01-01
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
  • 1970-01-01
  • 2012-12-02
相关资源
最近更新 更多