java 单击并拖动Selenium(chrome webdriver)不是在拖动,而是会单击并按住

35g0bw71  于 2023-02-07  发布在  Java
关注(0)|答案(4)|浏览(175)

所以我试着自动化一个列表元素,它可以被点击,拖到ol元素的不同部分,然后保存。但是测试只会停留在元素上。它不会移动偏移量,也不会移动到目标元素。
Chrome网页驱动程序,Java/ selenium

public void clickAndDragListElement() {
    Actions hold = new Actions(driver);
    hold.clickAndHold(targetHoldElement)
        .moveToElement(targetDestinationElement)
        .release(targetHoldElement)
        .build()
        .perform();
}

(WebElement在元素外部定义)

ogq8wdun

ogq8wdun1#

new Actions(driver)
                .moveToElement(source)
                .pause(Duration.ofSeconds(1))
                .clickAndHold(source)
                .pause(Duration.ofSeconds(1))
                .moveByOffset(1, 0)
                .moveToElement(destination)
                .moveByOffset(1, 0)
                .pause(Duration.ofSeconds(1))
                .release().perform();
3wabscal

3wabscal2#

你有没有试过这样的东西:

// Create object of actions class
Actions act=new Actions(driver);

// find element which we need to drag
WebElement drag=driver.findElement(By.xpath(".//*[@id='draggable']"));

// find element which we need to drop
WebElement drop=driver.findElement(By.xpath(".//*[@id='droppable']"));

// this will drag element to destination
act.dragAndDrop(drag, drop).build().perform();
afdcj2ne

afdcj2ne3#

我试过这个,它对我来说非常有效:

public class DragAndDrop {

    public static void main(String[] args) {
         System.setProperty("webdriver.chrome.driver", "C:\\Users\\Ranosys\\workspace\\MyTest\\chromedriver.exe");
         WebDriver driver = new ChromeDriver();
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
          WebDriverWait wait=new WebDriverWait(driver,50 );

         driver.manage().window().maximize();
         driver.get("http://demo.guru99.com/test/drag_drop.html");
       //Element which needs to drag.           
        WebElement From=driver.findElement(By.xpath("//*[@id='credit2']/a"));   

      //Element on which need to drop.      
      WebElement To=driver.findElement(By.xpath("//*[@id='bank']/li"));                 

      //Using Action class for drag and drop.       
      Actions act=new Actions(driver);                  

    //Dragged and dropped.      
      act.dragAndDrop(From, To).build().perform();  

    }

}
hfsqlsce

hfsqlsce4#

这些解决方案对我都不起作用。@Fenio的建议似乎是我的用例最大的希望,但我没有运气。相反,我决定使用selenium来获得元素的坐标,然后使用pyautogui(内部使用Xlib)来执行点击和鼠标移动,而non-headless webdriver运行在kiosk模式下。这个迂回的解决方案对我起作用了。
(顺便说一句,我在python中使用 selenium 元素)

相关问题