Selenium-Implementation of file uploads-for uploads that contain INPUT element

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.