How to handle with uploading files from modal wind

2020-06-27 09:03发布

I need to upload file. Steps are: 1. Clicking on on button which cal modal Window. (Linux window has no location bar) 2. To choose right file and upload it.

Very appreciate any help. Using Chrome Firefox drivers and Java.

3条回答
够拽才男人
2楼-- · 2020-06-27 09:44

I find workaround with Robot class

here is code:

try {
    Robot robot = new Robot();

    robot.delay(3000);
    robot.keyPress(KeyEvent.VK_P);      
    robot.keyRelease(KeyEvent.VK_P);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
    } catch (AWTException e) {
    e.printStackTrace();

    }
查看更多
一纸荒年 Trace。
3楼-- · 2020-06-27 09:56

For modal windows, I prefer using autoit with selenium, autoit is a very light weight application and can create the script and compile to make an exe file and run the exe file in your selenium,

Formore information see here.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-06-27 10:01

This has been asked several times and is also in some Selenium FAQ.

// assuming driver is a well instantiated WebDriver
WebElement fileInput = driver.findElement(By.xpath("//input[@type='file']"));
fileInput.sendKeys("C:/path/to/file.jpg");

The idea is to directly send the path to the file to an element which you would usually click at to get the modal window - that is <input type='file' /> element.

查看更多
登录 后发表回答