0

How to upload files from local via window prompt using selenium webdriver?

I want to perform the following actions:

  1. click on 'Browse' option on the window
  2. from the window prompt go to the particular location in the local where the file is kept
  3. select the file and click on 'Open' to upload the file.
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
danish
  • 41
  • 2
  • 3
  • 5
  • Refer below post. http://stackoverflow.com/questions/9431978/one-solution-for-file-upload-using-selenium-webdriver-with-java – Santoshsarma Mar 25 '15 at 13:25
  • i already tried that.. i m getting an error "VK_CONTROL cannot be resolved or is not a field".. M getting similar errors for VK_V and VK_ENTER also.. – danish Mar 25 '15 at 13:28
  • Autoit can also help you in this. Its used for controlling the windows applications. We have used Auto it many times in our projects. Give a try to that. In case of any help, please post here4 – Anil Chandna Mar 25 '15 at 19:14

3 Answers3

1

Have you tried using input() on proper file input control?

WebElement fileInput = driver.findElement(By.id("some id"));
fileInput.sendKeys("C:/path/to/file.extension");
Saifur
  • 16,081
  • 6
  • 49
  • 73
  • yes i tried this... this is only allowing the window prompt to select.. but i am not able to select the file from the prompt by giving the location and file name... – danish Mar 25 '15 at 13:37
  • Can you provide the `html`? – Saifur Mar 25 '15 at 13:39
1

I have used below three different ways to upload a file in selenium webdriver.

  1. First simple case of just finding the element and typing the absolute path of the document into it. But we need to make sure the HTML field is of input type. Ex:<input type="file" name="uploadsubmit">

Here is the simple code:

    WebElement element = driver.findElement(By.name("uploadsubmit"));
    element.sendKeys("D:/file.txt");
    driver.findElement(By.name("uploadSubmit"));
    String validateText = driver.findElement(By.id("message")).getText();
    Assert.assertEquals("File uploaded successfully", validateText);
  1. Second case is uploading using Robot class which is used to (generate native system input events) take the control of mouse and keyboard.

  2. The the other option is to use 'AutoIt' (open source tool).

You can find the above three examples : - File Uploads with Selenium Webdriver

Dev Raj
  • 650
  • 2
  • 7
  • 18
0

Selenium Webdriver doesn't really support this. Interacting with non-browser windows (such as native file upload dialogs and basic auth dialogs) has been a topic of much discussion on the WebDriver discussion board, but there has been little to no progress on the subject.

I have, in the past, been able to work around this by capturing the underlying request with a tool such as Fiddler2, and then just sending the request with the specified file attached as a byte blob.

If you need cookies from an authenticated session, WebDriver.magage().getCookies() should help you in that aspect.

edit: I have code for this somewhere that worked, I'll see if I can get ahold of something that you can use.

public RosterPage UploadRosterFile(String filePath){
        Face().Log("Importing Roster...");

        LoginRequest login = new LoginRequest();
        login.username = Prefs.EmailLogin;
        login.password = Prefs.PasswordLogin;
        login.rememberMe = false;
        login.forward = "";
        login.schoolId = "";

        //Set up request data
        String url = "http://www.foo.bar.com" + "/ManageRoster/UploadRoster";
        String javaScript = "return $('#seasons li.selected') .attr('data-season-id');";
        String seasonId = (String)((IJavaScriptExecutor)Driver().GetBaseDriver()).ExecuteScript(javaScript);
        javaScript = "return Foo.Bar.data.selectedTeamId;";
        String teamId = (String)((IJavaScriptExecutor)Driver().GetBaseDriver()).ExecuteScript(javaScript);

        //Send Request and parse the response into the new Driver URL
        MultipartForm form = new MultipartForm(url);
        form.SetField("teamId", teamId);
        form.SetField("seasonId", seasonId);
        form.SendFile(filePath,LoginRequest.sendLoginRequest(login));
        String response = form.ResponseText.ToString();
        String newURL = StaticBaseTestObjs.RemoveStringSubString("http://www.foo.bar.com" + response.Split('"')[1].Split('"')[0],"amp;");

        Face().Log("Navigating to URL: "+ newURL);
        Driver().GoTo(new Uri(newURL));

        return this;
    }

Where MultiPartForm is: MultiPartForm

And LoginRequest/Response: LoginRequest LoginResponse

The code above is in C#, but there are equivalent base classes in Java that will do what you need them to do to mimic this functionality.

The most important part of all of that code is the MultiPartForm.SendFile method, which is where the magic happens.

aholt
  • 2,829
  • 2
  • 10
  • 13