Selenium2 (Java) Selenium common API Five

Source: Internet
Author: User
Tags save file

Uploading files

The element tag is the upload method when input is

Upload is an input, for this element is input

Tags can be directly assigned using the SendKeys () method of the element, upload.html the code:

<HTML>    <Head></Head>    <Body>        <inputtype= "File"ID= "Filecontrol"value= "Select File">    </Body>    <styletype= "Text/css"ID= "1"></style></HTML>

Specific code:

Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.firefox.FirefoxDriver; Public classUpload1 { Public Static voidMain (string[] args) {Webdriver driver=NewFirefoxdriver (); //Open the uploaded Web page-get to enter the address of uploadDriver.get ("d:\\work\\eclipse_workspace\\seleniumdemo\\res\\upload.html"); Webelement E1= Driver.findelement (By.id ("Filecontrol")); //Enter the address where you want to upload the fileE1.sendkeys ("d:\\work\\eclipse_workspace\\seleniumdemo\\res\\ uploaded file. txt"); Try {        //wait 2 seconds to observe the displayThread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace ();        } driver.quit (); }}

Using AutoIt Script Upload method

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.

AutoIt installation package Address link: http://pan.baidu.com/s/1dDL5Qhz Password: yew5

Binary file installation, open AutoIt, the main interface is as follows:

Specific use:

Drag the Finder tool to the pop-up window you want to locate

Gets the classname of the file name input box

Gets the classname of the Open button

Write the AutoIt script based on the obtained value:

; fisrt make sure the number of arguments passed into the script isMore than1If $CmdLine [0]<2ThenExitElseIf; If Parmas num<2, then break; cmdline[0]; the number of parameters; cmdline[1]; The first argument (after the name of the script); cmdline[2]; the second argument, which is passed from CMD to the parameter Handleupload ($CmdLine [1], $CmdLine [2]); Define the upload function, there are two parameters, the first one is the name of the browser, the second parameter is the file path func handleupload ($browser, $uploadfile) Dim $title; define a title variable; Judging by the title of the pop-up window, what is the browser If $browser="ie "then; on behalf of Chrome browser$title ="Select the file to load "ElseIf $browser ="Chrome "then;$title ="Open theElseIf $browser ="Firefox " Then$title ="File Upload "EndIf If winwait ($title,"", 4) Wait for the pop-up window to appear, the maximum wait time is 4swinactivate ($title); After the popup window is found, activate the current window Controlsettext ($title,"" ," Edit ", $uploadfie); Put the file path into the input boxControlclick ($title,"" ," button "); Click Save or Open or uploadElse ReturnFalseEndifendfunc

Save file as UPLOAD.AU3, convert file to exe executable file

C:\Program Files under the installation directory (x86) \autoit3\aut2exe\aut2exe_x64.exe

Generate File Upload.exe

Open cmd, type: "C:\Users\Administrator\Desktop\upload.exe" "Firefox" "C:\Users\Administrator\Desktop\image.jpg" double quotation marks do not omit

Call the script in selenium, encapsulating it as a way to execute the exe file:

/*** Upload files, need to click the popup upload window * *@paramBrowser * Name of the browser used *@paramfile * files and filenames that need to be uploaded*/ Public voidhandleupload (String browser, file file) {string filepath=File.getabsolutepath (); String Executefile= "C:\Users\Administrator\Desktop\upload.exe";//defines the path to the Upload.exe fileString cmd = "\" "+ Executefile +" \ "+" "+" \ "+ browsser +" \ "+" + "\" "+ FilePath +" \ ""; Try{Process P=runtime.getruntime (). exec (CMD);        P.waitfor (); } Catch(Exception e) {e.printstacktrace (); }}

The concrete steps are: first 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 (Java) Selenium common API Five

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.