"Automation topic" selenium how to easily handle file uploads

Source: Internet
Author: User

When using selenium to automate, one of the headaches that we often encounter is file uploads.

The difficulty of the problem is that selenium cannot recognize and manipulate the Windows window, and if we can bypass the popup box and pass the file information directly to the selection button, the difficulty is solved.

the following are common page forms :

parsing HTML, found that both the normal foreground and the ExtJS, their HTML has a common feature, with the following code:

  <type= "file"  ... />

OK, as long as we use Selenium's own SendKeys to pass the file path (relative absolutely indifferent) to the element, you can bypass the selection window to upload files directly.

Solution was born

Driver.findelement (XXXXX). SendKeys ("FilePath");

Have a Try

Blog Park upload Image: Driver.findelement (by.name ("file")). SendKeys ("C:\Users\123\Pictures\a.png");

QQ Mail Upload attachment: driver.findelement (By.name ("UploadFile")). SendKeys ("C:\Users\123\Pictures\a.png");

Positioning elements depending on the situation, does not necessarily use name; file path can make relative path, everyone modify it.

Note: execute SendKeys elements must conform to input and type= "file" condition, otherwise you do not find the upload file to the object, will upload failed.

Like this, must be careful not to upload to the front of the input, otherwise it will fail

Automation topic Selenium How to easily handle file uploads

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.