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