How to handle file upload in selenium in windows 7

那年仲夏 提交于 2019-12-25 06:37:26

问题


I am writing a script in selenium using webdriver and nunit to automate my web application. I have to upload file in my application. But unable to do it.

The dialog opens while browse button is clicked, and upon selecting file the file is uploaded.

Browse

How can i do it in selenium?


回答1:


Answer is already available on stack, still I am repeating:

find the upload button, create webelement of this and follow this code:

   //open upload window
    upload.click();

    //put path to your image in a clipboard
    StringSelection ss = new StringSelection(<give file location>);
    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);



回答2:


public void uploadFile(String locatorId, String filePath) {

WebElement fileInput = findElementExplicitWaitWithNoVisbility(By.id(locatorId), true);

fileInput.clear();
fileInput.sendKeys(filePath);}


来源:https://stackoverflow.com/questions/32760330/how-to-handle-file-upload-in-selenium-in-windows-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!