Uploading files
There are two ways to implement the upload feature for Web pages:
- Normal upload: Place the path of the local file as a value in the Input tab and submit this value to the server via the form form (no introduction to the Send_keys method).
- AutoIt Upload: Automate tasks with analog keyboard keys, mouse Movements and window/control combinations.
Let's take a practical look at the process of AutoIt uploading files:
1, install AutoIt (download website: https://www.autoitscript.com/site/autoit/downloads/)
2. Open the AutoIt Windows Info tool (My computer is 64-bit)
3, the left mouse button click the pop-up window finder Tool, the mouse will become a small fan-shaped icon
4. Hold the Finder tool loose and drag it to the control you want to identify
The title of the window is "file Upload" with the class "#32770"
The file name input box is class "Edit" and instance is "1", so Classnamenn is "ComboBox1"
The Open button's class is "button", instance is "1", so Classnamenn is "Button1"
5. Open the SciTE Script Editor and write AutoIt scripts according to the control information that AutoIt Windows info recognizes.
;ControlFocus("title","text",controlID) ComboBox1=Combobox instance 1ControlFocus("文件上传", "" , "Edit1");Wait 10 Seconds for the Upload window to appearWinWait("[CLASS:#32770]", "", 10);Set the file name text on the Edit fieldControlSetText("文件上传", "", "Edit1", "alert.html")Sleep(2000);Click on the open buttonControlClick("文件上传", "", "Button1")
6. After saving the file, open the Compile Script to.exe tool to generate it as an. exe executable file
Watch out.
- Different browser window title may not be the same
- File name input box do not navigate to the drop-down box.
- Note whether the upload path delimiter is a single slash or double slash
- EXE execution time, execution error, automation scripts are not known (not controllable range)
* * Code Time **java
1 Packagejavatest;2 3 Importjava.io.IOException;4 Importjava.util.NoSuchElementException;5 ImportJava.util.concurrent.TimeUnit;6 Importorg.openqa.selenium.By;7 ImportOrg.openqa.selenium.WebDriver;8 ImportOrg.openqa.selenium.firefox.FirefoxDriver;9 Ten Public classTest { One Public Static voidMain (string[] arg)throwsinterruptedexception, IOException A { -Webdriver Driver =Newfirefoxdriver (); - the //Set implicit wait Duration: 10 seconds; -Driver.manage (). Timeouts (). Implicitlywait (10, timeunit.seconds); -Driver.get ("http://www.jq22.com/yanshi5310"); - + Try { -Driver.switchto (). FRAME ("iframe"); +Driver.findelement (By.xpath ("//*[@id = ' upload_form ']/div[1]/div[2]"). Click ();//Open the Upload window ARuntime.getruntime (). EXEC ("C:\\users\\xxx\\desktop\\upfile.exe");//call Upfile.exe Upload program at } - Catch(nosuchelementexception e) - { - System.out.println (E.getmessage ()); - } - finally in { - driver.close (); to } + } -}
Python
1 fromSeleniumImportWebdriver2 fromSelenium.webdriver.common.byImport by3 ImportOS4 5 #start the Firefox browser6Driver =Webdriver. Firefox ()7 8 #implicitly waits for 10S, opens the URL (can be positioned directly through the frame's ID and name)9Driver.implicitly_wait (10)TenDriver.get ("http://www.jq22.com/yanshi5310") One A Try: -Driver.switch_to.frame ("iframe") -Driver.find_element (By.xpath,"//*[@id = ' upload_form ']/div[1]/div[2]"). Click ()#Open the Upload window theOs.system ("C:\\users\\xxx\\desktop\\upfile.exe")#call Upfile.exe Upload program - exceptException as E: - Print(E.args[0]) - finally: +Driver.close ()
Ruby
1 classBaidu2Require'RubyGems'3Require'Selenium-webdriver'4 5 #Open Firefox and enter the URL6Driver = Selenium::webdriver. for: Firefox7 8 #set an implicit wait time of 10S9driver.manage.timeouts.implicit_wait = 10TenDriver.navigate.to"http://www.jq22.com/yanshi5310" One A begin -Driver.switch_to.frame ('iframe') -Driver.find_element (: XPath = ="//*[@id = ' upload_form ']/div[1]/div[2]"). Click#Open the Upload window the exec("C:\\users\\xxx\\desktop\\upfile.exe") -Rescue =e -Puts E.message#Show error message - ensure + Driver.close - End +End
Download file
Words Don't say much,< ( ̄︶ ̄) [go!]
FireFox about:config Detailed Description: https://www.cnblogs.com/abcd19880817/p/7210711.html
* * Code Time **java
Firefoxprofile fp = new Firefoxprofile ();//0 is the download to the default download path of the browser, 1 is "my download"; 2 is the custom Fp.setpreference (" Browser.download.folderList ", 2);//whether to show start fp.setpreference (" Browser.download.manager.showWhenStarting ", false);// Specifies the directory for the downloaded file fp.setpreference ("Browser.download.dir", "d:\\");//download file type Fp.setpreference (" Browser.helperApps.neverAsk.saveToDisk "," Application/octet-stream ");
Python
FP =Webdriver. Firefoxprofile () fp.set_preference ("browser.download.folderList", 2) Fp.set_preference ("browser.download.manager.showWhenStarting", False)#Specifies the directory for the downloaded file. The OS.GETCWD () function does not need to pass parameters to return the current directoryFp.set_preference ("Browser.download.dir", OS.GETCWD ()) Fp.set_preference ("Browser.helperApps.neverAsk.saveToDisk","Application/octet-stream")
Ruby
Profile = Selenium::webdriver::firefox::P rofile.newprofile['browser.download.folderList '] = 2profile['browser.download.manager.showWhenStarting') = = Selenium::webdriver. for: Firefox,:p rofile = profile
"Web Automation Test-code Eight" common methods-upload/download files