First, for uploading files, from the manual operation we can see that the window form needs to operate, and for the selenium webdriver in this area of application is limited. However, fortunately, for the upload containing the input element, we can pass the file path directly through the SendKeys, omitting the operation of the window form to implement the file upload, the implementation process is as follows:
1) Locate the upload Control element and enter the path:
webelement element = Driver.findelement (By.id ("Cloudfax-attachment-form-upload-input"));
Element.sendkeys (GetFilePath (text.txt));
2) Processing of the path:
private string GetFilePath (string resource) {
URL path = This.getclass (). getresource (Resource);
Return path.tostring (). ReplaceAll ("file:/", "" ");
}
By uploading the code and files to the server, you can find the file and upload it.
Here are a few things to note:
- The element want to put FilePath is the "whole" INPUT element rather than the "readonly" input element,as below, the F Irst Locator is right but the second locator would throw an exception.
- Call Element.sendkeys directly, do not need to do element.clear () again, otherwise the exception will appear: Element must is user-editable in order to clear it.
Second, for the following control upload method, temporarily unable to achieve:
Selenium-Implementation of file uploads-for uploads that contain INPUT element