Upload files using selenium

孤者浪人 提交于 2019-12-17 16:41:56

问题


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.

回答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");



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/29256865/upload-files-using-selenium

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