Selenium2 Uploading Files

Source: Internet
Author: User

1. The label is input, as shown in:

Webelement e1= driver.findelement (by.id ("load"));
Enter the address where you want to upload the file
E1.sendkeys ("d:\\work\\eclipse_workspace\\seleniumdemo\\res\\ uploaded file. txt");

2. Using AutoIt Script Upload method

Some upload controls are not input elements, and using the SendKeys method is not feasible at this time. At this point can be autoit to help us implement the upload function, first understand what is AutoIt. It is a free software that uses a similar basic scripting language and is designed for automated operations in the Windows GUI (graphical user interface). It uses analog keyboard keys, mouse Movement and window/control combinations to automate tasks. And this is something that other languages are unlikely to do or have no reliable means to achieve.

We mainly use AutoIt to achieve the location of the upload file pop-up window, and then to the pop-up window Address box Assignment (file path), and then click the OK button to upload the file, such as:

Specific ideas to know how to do, the following through the AutoIt script to achieve.
First you need to install AutoIt (the installation file under the Tools folder in the network disk), directly installed by default.
Next you need to go through AutoIt window info (x86) or AutoIt window info (x64) (This is the AutoIt app you just installed, you can find it in the Start menu, if it's not in the Start menu, look in the installation directory), Open AutoIt Window Info First,

Open the program that corresponds to your windows, with the following interface:

This tool is used to locate Windows GUI (Windows interface) elements. The Upload pop-up box is then positioned.

That Baidu's personal avatar upload function, click Select Image:

After that, it pops up:

Then you can switch to the AutoIt window Info tool that you just opened, and find finder tools: Then drag the icon to the file upload pop-up box with your mouse:

Record the classname value of the file name input box. Then use the method above to get the calssname of the "open" button,

Record the calssname of the Open button. Next, write the AutoIt script based on the positional values.

Open the installation directory: SciTE Script Editor Application:

Enter the following code:

;First make sure the number of arguments passed to the scripts is more than 1If $CmdLine [0]<2Then Exit EndIf;if Parmas num <2, then break;$CmdLine [0]; number of parameters;$CmdLine [1]; First argument (after script name);$CmdLine [2]; second parameter;are passed from CMD to the parameterHandleupload ($CmdLine [1], $CmdLine [2]);define the upload function, there are two parameters, the first is the browser name, the second parameter is the file pathFunc handleupload ($browser, $uploadfile) Dim $title;define a title variable            ;Judging by the title of the pop-up window, what browser?If $browser ="IE"Then;On behalf of IE browser$title ="Select the files to load"ElseIf $browser ="Chrome"Then;On behalf of Google browser$title ="Open"ElseIf $browser ="Firefox"Then;On behalf of the Firefox browser$title ="File Upload"EndIf If Winwait ($title,"",4) Then;wait for the popup to appear, the maximum wait time is 4 secondsWinactivate ($title);activates the current window after a popup window is foundControlsettext ($title,"","Edit1", $uploadfile);put the file path into the input box, this "Edit1" is obtained by FindertoolControlclick ($title,"","Button1");Click the Save or Open or upload button, this "Button1" uses Findertool to obtain theElse Return False EndIf endfunc

It should be stressed that AutoIt is based on the name of the popup window to the name of the input box and click on the button, and so on, because different browser pop-up name may not be the same, here to choose carefully.

AutoIt's script is finished, but since we're calling it in cmd, we need to convert it to an EXE executable file.

Open the application under the AutoIt installation directory: Compile script to. exe (x86) or Compile script to. exe (x64).

Then follow the steps in the diagram:

Click the Conver button to successfully translate the UPLOAD.AU3 script to run the file Upload.exe, such as:

Next Test the effect, open cmd, enter: "C:\Users\Jing\Documents\upload.exe" "Firefox" "C:\Users\Public\Pictures\pic.jpg" double quotation marks do not omit,

Then go back to the webpage and see that the avatar has been uploaded:

Here need to emphasize the script in the CMD trial, the first parameter is the AutoIt exe script file, the second is the AutoIt script function required parameters, look back to the script, a parameter is the browser name: Chrome or IE or Firefox, the second parameter is to upload the path of the file, And you have to use the double quotation marks in the English environment to form the following code:
"C:\Users\Jing\Documents\upload.exe" "Firefox" "C:\Users\Public\Pictures\pic.jpg"

How do I invoke the Eat script in selenium? You can encapsulate it as a way to execute the exe file:

/*** Upload a file, you need to click the Pop-up window to upload photos * * * * * * * * * * * * * * * * * @parambrower * @paramfile * files and file names to upload*/publicvoidhandleupload (String browser, file file) {string FilePath=File.getabsolutepath (); String Executefile= "Res/script/autoit/upload.exe";//defines the Upload.exepath to file String cmd= "\" "+ executefile+" \ "+" "+" \ "+ browser+" \ "+" + "\" "+ filepath+" \ ""; Try{Process P=runtime.getruntime (). exec (CMD);    P.waitfor (); } Catch(Exception e) {e.printstacktrace (); }}

Then call in the use case where you want to upload the file, with the following steps:

First go to use Selenium click Upload File button, then will pop up the upload file input box, and finally execute the above public void Handleupload (String browser, file file) method can be.

Selenium2 Uploading files

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.