gpt4 book ai didi

java - 使用 Selenium 和 Java 可以通过多少种方式实现拖放功能

转载 作者:行者123 更新时间:2023-12-01 18:30:53 25 4
gpt4 key购买 nike

我们可以通过多少种方式使用 Selenium 和 Java 实现拖放功能?

代码尝试:

Thread.sleep(3000);
WebElement FROM = driver.findElement(By.xpath("(//div[@class='item-container flex-container-horizontal'])[1]"));
Thread.sleep(3000);
WebElement to = driver.findElement(By.xpath("//div[text()='Product Quality?']"));
Thread.sleep(3000);
Actions act=new Actions(driver);
act.dragAndDrop(FROM, to).build().perform();

最佳答案

拖放功能可以通过多种方式实现,如下所示:

  • 使用dragAndDrop():

    System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
    WebDriver driver=new FirefoxDriver();
    driver.get("http://jqueryui.com/droppable/");
    driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']")));
    WebElement from = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("draggable")));
    WebElement to = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("droppable")));
    new Actions(driver).dragAndDrop(from, to).build().perform();
  • 链接 clickAndHold()moveToElement():

    System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
    WebDriver driver=new FirefoxDriver();
    driver.get("http://jqueryui.com/droppable/");
    driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']")));
    WebElement drag = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("draggable")));
    WebElement drop = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("droppable")));
    new Actions(driver).clickAndHold(from).moveToElement(to).release(from).build().perform();
  • 视频演示:

dragdrop

关于java - 使用 Selenium 和 Java 可以通过多少种方式实现拖放功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60168264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com