簡體   English   中英

使用java將圖像拖放到BOX-selenium中

[英]Drag and drop the image into the BOX- selenium with java

我想使用 selenium 在 W3school 網頁中執行拖放操作。 代碼工作正常,但輸出未顯示在網頁上。

鏈接是:- http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop

我的代碼是:-

public String dragAndDrop(String object,String data){
    APP_LOGS.debug("waiting for popup closer");
    try{

        driver.switchTo().frame("iframeResult");
        WebElement element = driver.findElement(By.xpath(".//*[@id='drag1']"));
        WebElement target = driver.findElement(By.xpath(".//*[@id='div1']"));
        (new Actions(driver)).dragAndDrop(element, target).build().perform();
    }catch(Exception e){
        return Constants.KEYWORD_FAIL+" -- Unable to drag"+e.getMessage();

    }

    return Constants.KEYWORD_PASS;
}

我們還可以使用 Selenium 中的 Actions 類和機器人類與鍵盤/鼠標事件交互。 我已經使用Robot類來解決您的問題。

Robot類存在於java.awt包中。 您可以檢查文檔所有方法。

public static void Task1() throws AWTError, AWTException, InterruptedException
{
    WebDriver driver = new FirefoxDriver();

    driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop");

    driver.switchTo().frame("iframeResult");
    WebElement element1 = driver.findElement(By.xpath(".//img[@id='drag1']"));
    WebElement element2 = driver.findElement(By.xpath(".//*[@id='div1']"));
    Actions action = new Actions(driver);

    Point element3 = driver.findElement(By.xpath(".//*[@id='drag1']")).getLocation();
    int i=element3.getX()+800;
    int b=element3.getY()+250;

    Robot robot = new Robot();
    robot.mouseMove(i, b);
    // Press left click of mouse
    robot.mousePress( InputEvent.BUTTON1_DOWN_MASK);
    robot.delay(4000);
    robot.mouseMove(i+20, b-120);

    robot.mousePress( InputEvent.BUTTON1_DOWN_MASK);

    robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

    Thread.sleep(10000);
    driver.close();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM