How to upload file using Selenium WebDriver in Java

后端 未结 6 1111
逝去的感伤
逝去的感伤 2020-11-22 10:34

Can anyone let me know how to upload a file using Selenium by Java code?

When I click on button in the application it gets open in the new window what I can use to s

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 11:07

    This is what I use to upload the image through upload window:

        //open upload window
        upload.click();
    
        //put path to your image in a clipboard
        StringSelection ss = new StringSelection("C:\\IMG_3827.JPG");
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
    
        //imitate mouse events like ENTER, CTRL+C, CTRL+V
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
    

    done

提交回复
热议问题