標籤:browser star wow 圖片 傳遞參數 script win drive timeout
上傳檔案
對於Web頁面的上傳功能實現一般有一下倆種方式:
- 普通上傳:將本地檔案的路徑作為一個值放在input標籤中,通過form表單將這個值提交給伺服器(不做介紹send_keys方法)。
- AutoIt上傳:利用類比鍵盤按鍵,滑鼠移動和視窗/控制項的組合來實現自動化任務。
下面我們實際操作一下來講解AutoIt上傳檔案的過程:
1、安裝AutoIt(下載網址:https://www.autoitscript.com/site/autoit/downloads/)
2、開啟AutoIt Windows Info工具(我的電腦是64位的)
3、用滑鼠左鍵點擊快顯視窗的Finder Tool,此時滑鼠將變成一個小風扇形狀的表徵圖
4、按住Finder Tool不松,將其拖動到需要識別的控制項上
視窗的title為“檔案上傳”,標題的class為“#32770”
檔案名稱輸入框的class為“Edit”,Instance為“1”,所以classnameNN為“ComboBox1”
開啟按鈕的class為“Button”,Instance為“1”,所以classnameNN為“Button1”
5、根據AutoIt Windows Info所識別到的控制項資訊開啟SciTE Script Editor編輯器,編寫AutoIt指令碼。
;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、儲存檔案後,開啟Compile Script to.exe工具,將其產生為.exe可執行檔
注意點
- 不同瀏覽器的視窗title有可能不一樣
- 檔案名稱輸入框不要定位到下拉框上去
- 注意上傳路徑的分隔字元是單斜杠還是雙斜杠
- exe執行多長時間,執行是否出錯,自動化指令碼都無法得知(不在可控範圍內)
**代碼時間 **Java
1 package JavaTest; 2 3 import java.io.IOException; 4 import java.util.NoSuchElementException; 5 import java.util.concurrent.TimeUnit; 6 import org.openqa.selenium.By; 7 import org.openqa.selenium.WebDriver; 8 import org.openqa.selenium.firefox.FirefoxDriver; 9 10 public class Test {11 public static void main(String[] arg) throws InterruptedException, IOException12 {13 WebDriver driver = new FirefoxDriver();14 15 // 設定隱示等待時間長度:10秒;16 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);17 driver.get("http://www.jq22.com/yanshi5310"); 18 19 try {20 driver.switchTo().frame("iframe"); 21 driver.findElement(By.xpath("//*[@id=‘upload_form‘]/div[1]/div[2]")).click(); //開啟上傳視窗22 Runtime.getRuntime().exec("C:\\Users\\xxx\\Desktop\\upfile.exe"); // 調用upfile.exe上傳程式23 }24 catch(NoSuchElementException e)25 {26 System.out.println(e.getMessage());27 }28 finally29 {30 driver.close();31 } 32 }33 }
Python
1 from selenium import webdriver 2 from selenium.webdriver.common.by import By 3 import os 4 5 # 啟動Firefox瀏覽器 6 driver = webdriver.Firefox() 7 8 # 隱式等待10S,開啟網址(可直接通過frame的id和name定位) 9 driver.implicitly_wait(10)10 driver.get("http://www.jq22.com/yanshi5310")11 12 try:13 driver.switch_to.frame("iframe")14 driver.find_element(By.XPATH, "//*[@id=‘upload_form‘]/div[1]/div[2]").click() # 開啟上傳視窗15 os.system("C:\\Users\\xxx\\Desktop\\upfile.exe") # 調用upfile.exe上傳程式16 except Exception as e:17 print(e.args[0])18 finally:19 driver.close()
Ruby
1 class Baidu 2 require ‘rubygems‘ 3 require ‘selenium-webdriver‘ 4 5 # 開啟firefox並輸入網址 6 driver = Selenium::WebDriver.for :firefox 7 8 # 設定隱式等待時間10S 9 driver.manage.timeouts.implicit_wait = 1010 driver.navigate.to "http://www.jq22.com/yanshi5310"11 12 begin13 driver.switch_to.frame(‘iframe‘)14 driver.find_element(:xpath => "//*[@id=‘upload_form‘]/div[1]/div[2]").click # 開啟上傳視窗15 exec("C:\\Users\\xxx\\Desktop\\upfile.exe")16 rescue => e17 puts e.message # 顯示報錯資訊18 ensure19 driver.close20 end21 end
下載檔案
話不多說,<( ̄︶ ̄)[GO!]
FireFox about:config詳細介紹:https://www.cnblogs.com/abcd19880817/p/7210711.html
**代碼時間 **Java
FirefoxProfile fp = new FirefoxProfile();// 為0是下載到瀏覽器預設下載路徑,1是“我的下載”;2是自訂fp.setPreference("browser.download.folderList", 2);// 是否顯示開始fp.setPreference("browser.download.manager.showWhenStarting", false);// 指定所下載檔案的目錄fp.setPreference("browser.download.dir", "d:\\");// 下載檔案類型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)# 指定所下載檔案的目錄。os.getcwd()函數不需要傳遞參數,用於返回當前的目錄fp.set_preference("browser.download.dir",os.getcwd())fp.set_preference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream")
Ruby
profile = Selenium::WebDriver::Firefox::Profile.newprofile[‘browser.download.folderList‘] = 2profile[‘browser.download.manager.showWhenStarting‘] = falsedriver = Selenium::WebDriver.for :firefox, :profile => profile
【Web自動化測試——代碼篇八】常用方法——上傳/下載檔案