如何使用 Selenium 按降序拖放项目
How To Drag And Drop the Items in Decending order using Selenium
嗨,我是硒新手,
In URL: https://jqueryui.com/sortable/ 在鼠标的帮助下我们需要对列表进行排序。我尝试了这段代码,但在 selenium 中没有任何解决方法我们如何才能做到这一点?
有7条数据需要用鼠标降序排列
我尝试了下面的代码,但它不起作用我该如何实现它
WebDriver driver =new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://jqueryui.com/sortable/");
driver.switchTo().frame(0);
List<WebElement> lists = driver.findElements(By.xpath("//ul[@id='sortable']/li"));
Actions a = new Actions(driver);
for(int i=0;i<lists.size();++i){
WebElement element = lists.get(i);
String text = lists.get(i).getText();
String[] values = text.split(" ");
int number = Integer.valueOf(values[1]);
}
a.clickAndHold(lists.get(0)).dragAndDrop(lists.get(0), lists.get(6)).build().perform();
a.clickAndHold(lists.get(0)).moveToElement(lists.get(3)).release().build().perform();
////////////////////////////////////////// //////////////////
for(int i =dragAndDropElement.size();i>1;i--) {
WebElement element = driver.findElement(By.xpath("((//ul[@id='sortable']/li)["+i+"])"));
//Just collected all the destination location,
WebElement destination1 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[1])"));
WebElement destination2 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[2])"));
WebElement destination3 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[3])"));
WebElement destination4 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[4])"));
WebElement destination5 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[5])"));
WebElement destination6 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[6])"));
WebElement destination7 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[7])"));
Actions action = new Actions(driver);
if(element!=null) {
action.dragAndDrop(destination1,element).perform();
action.dragAndDrop(destination2,element).perform();
action.dragAndDrop(destination3,element).perform();
action.dragAndDrop(destination4,element).perform();
action.dragAndDrop(destination5,element).perform();
action.dragAndDrop(destination6,element).perform();
action.dragAndDrop(destination7,element).perform();
break;
}
您可以试试这个代码:
List<WebElement> lists = driver.findElements(By.xpath("//ul[@id='sortable']/li"));
// above list holds size of 7
Actions a = new Actions(driver);
WebElement lastEle = driver.findElement(By.xpath("//li[text()='Item " + lists.size() + "']"));
// I am picking last element as we have to sort in descending order. That means 1-7, 2-7, 3-7 etc..
// At last the order should be Item 7, 6, 5, 4, 3, 2, 1.
for(int i=1;i<=lists.size() - 1;++i) {
// Here, I don't want to drag last element i.e Item 7 as it will be on top at last. That is why I am not considering 7th element to drag
WebElement elementToDrag = driver.findElement(By.xpath("//li[text()='Item " + i + "']"));
a.clickAndHold(elementToDrag).dragAndDrop(elementToDrag, lastEle).build().perform();
Thread.sleep(1000);
}
您可以检查这个解决方案,此外,您可以根据您的要求和简化来增强它。总之,希望对你有所帮助。
WebDriver driver = new ChromeDriver();
driver.get("https://jqueryui.com/sortable/");
new WebDriverWait(driver,10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='demo-frame']")));
List<WebElement> dragAndDropElement = driver.findElements(By.xpath("//ul[@id='sortable']/li"));
System.out.println(dragAndDropElement.size());
for(int i =1;i<dragAndDropElement.size();i++) {
WebElement element = driver.findElement(By.xpath("((//ul[@id='sortable']/li)["+i+"])"));
//Just collected all the destination location,
WebElement destination1 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[1])"));
WebElement destination2 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[2])"));
WebElement destination3 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[3])"));
WebElement destination4 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[4])"));
WebElement destination5 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[5])"));
WebElement destination6 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[6])"));
WebElement destination7 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[7])"));
Actions action = new Actions(driver);
if(element!=null) {
action.dragAndDrop(destination7,element).perform();
action.dragAndDrop(destination6,element).perform();
action.dragAndDrop(destination5,element).perform();
action.dragAndDrop(destination4,element).perform();
action.dragAndDrop(destination3,element).perform();
action.dragAndDrop(destination2,element).perform();
action.dragAndDrop(destination1,element).perform();
break;
}
}
嗨,我是硒新手,
In URL: https://jqueryui.com/sortable/ 在鼠标的帮助下我们需要对列表进行排序。我尝试了这段代码,但在 selenium 中没有任何解决方法我们如何才能做到这一点?
有7条数据需要用鼠标降序排列
我尝试了下面的代码,但它不起作用我该如何实现它
WebDriver driver =new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://jqueryui.com/sortable/");
driver.switchTo().frame(0);
List<WebElement> lists = driver.findElements(By.xpath("//ul[@id='sortable']/li"));
Actions a = new Actions(driver);
for(int i=0;i<lists.size();++i){
WebElement element = lists.get(i);
String text = lists.get(i).getText();
String[] values = text.split(" ");
int number = Integer.valueOf(values[1]);
}
a.clickAndHold(lists.get(0)).dragAndDrop(lists.get(0), lists.get(6)).build().perform();
a.clickAndHold(lists.get(0)).moveToElement(lists.get(3)).release().build().perform();
////////////////////////////////////////// ////////////////// for(int i =dragAndDropElement.size();i>1;i--) {
WebElement element = driver.findElement(By.xpath("((//ul[@id='sortable']/li)["+i+"])"));
//Just collected all the destination location,
WebElement destination1 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[1])"));
WebElement destination2 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[2])"));
WebElement destination3 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[3])"));
WebElement destination4 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[4])"));
WebElement destination5 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[5])"));
WebElement destination6 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[6])"));
WebElement destination7 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[7])"));
Actions action = new Actions(driver);
if(element!=null) {
action.dragAndDrop(destination1,element).perform();
action.dragAndDrop(destination2,element).perform();
action.dragAndDrop(destination3,element).perform();
action.dragAndDrop(destination4,element).perform();
action.dragAndDrop(destination5,element).perform();
action.dragAndDrop(destination6,element).perform();
action.dragAndDrop(destination7,element).perform();
break;
}
您可以试试这个代码:
List<WebElement> lists = driver.findElements(By.xpath("//ul[@id='sortable']/li"));
// above list holds size of 7
Actions a = new Actions(driver);
WebElement lastEle = driver.findElement(By.xpath("//li[text()='Item " + lists.size() + "']"));
// I am picking last element as we have to sort in descending order. That means 1-7, 2-7, 3-7 etc..
// At last the order should be Item 7, 6, 5, 4, 3, 2, 1.
for(int i=1;i<=lists.size() - 1;++i) {
// Here, I don't want to drag last element i.e Item 7 as it will be on top at last. That is why I am not considering 7th element to drag
WebElement elementToDrag = driver.findElement(By.xpath("//li[text()='Item " + i + "']"));
a.clickAndHold(elementToDrag).dragAndDrop(elementToDrag, lastEle).build().perform();
Thread.sleep(1000);
}
您可以检查这个解决方案,此外,您可以根据您的要求和简化来增强它。总之,希望对你有所帮助。
WebDriver driver = new ChromeDriver();
driver.get("https://jqueryui.com/sortable/");
new WebDriverWait(driver,10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='demo-frame']")));
List<WebElement> dragAndDropElement = driver.findElements(By.xpath("//ul[@id='sortable']/li"));
System.out.println(dragAndDropElement.size());
for(int i =1;i<dragAndDropElement.size();i++) {
WebElement element = driver.findElement(By.xpath("((//ul[@id='sortable']/li)["+i+"])"));
//Just collected all the destination location,
WebElement destination1 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[1])"));
WebElement destination2 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[2])"));
WebElement destination3 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[3])"));
WebElement destination4 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[4])"));
WebElement destination5 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[5])"));
WebElement destination6 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[6])"));
WebElement destination7 = driver.findElement(By.xpath("((//ul[@id='sortable']/li)[7])"));
Actions action = new Actions(driver);
if(element!=null) {
action.dragAndDrop(destination7,element).perform();
action.dragAndDrop(destination6,element).perform();
action.dragAndDrop(destination5,element).perform();
action.dragAndDrop(destination4,element).perform();
action.dragAndDrop(destination3,element).perform();
action.dragAndDrop(destination2,element).perform();
action.dragAndDrop(destination1,element).perform();
break;
}
}