2

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.

Vladimir Bosyi
  • 433
  • 1
  • 5
  • 7

3 Answers3

4

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.

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • thank, but in my case I have not free type input field. – Vladimir Bosyi May 04 '12 at 11:29
  • I guess that than I'm not really sure how you'd like to upload the file. Where to? Which button, what technology, what html source does it have? Is it on some publicly visible page? Can you post a minimal test case? Please, be more specific, we'd really like to help. – Petr Janeček May 04 '12 at 11:41
  • Thanks again but I pass this problem by asking developers make field in which I can add path. – Vladimir Bosyi May 04 '12 at 16:48
  • When you encounter a question which is an exact duplicate, please vote to close it instead of answering it. – oberlies Jun 05 '14 at 10:27
0

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();

    }
Girish Bellamkonda
  • 491
  • 1
  • 8
  • 17
Vladimir Bosyi
  • 433
  • 1
  • 5
  • 7
0

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.

Shai
  • 111,146
  • 38
  • 238
  • 371
sunpat
  • 389
  • 2
  • 7
  • 28