File Upload using Selenium WebDriver and Java Robot Class

后端 未结 7 1845
说谎
说谎 2020-11-28 07:19

I am using Selenium WebDriver and Java and I need to automate the file upload feature. I tried a lot, but the moment the Browse button is clicked and a new window opens the

7条回答
  •  渐次进展
    2020-11-28 07:50

    I think I need to add something to Alex's answer.

    I tried to open the Open window by using this code:

    driver.findElement(My element).click()
    

    The window opened, but the driver became unresponsive and the actions in the code didn't even get to the Robot's actions. I do not know the reason why this happens, probably because the browser lost focus.

    The way I made it work was by using the Actions selenium class:

     Actions builder = new Actions(driver);
    
     Action myAction = builder.click(driver.findElement(My Element))
           .release()
           .build();
    
        myAction.perform();
    
        Robot robot = new Robot();
        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);
    

提交回复
热议问题