当iframe引用其它页面时,需要一层一层的选择!
<iframe> <frameset name='fset'> <frame src = "..."/> <frame src = "..."> ... <input type='file'> ... </frame> </frameset> </frame>
1. ClickBrowseBtn.au3 功能描述:点击浏览按钮
#include <IE.au3> ;获取IE对象 $oIE = _IEAttach ("IE标题") forEachFrame($oIE) Func forEachFrame($oIFrame) ;查找FileInput控件 $oElements = _IETagNameGetCollection ($oIFrame, "input") For $oElement In $oElements If $oElement.type == 'file' Then ;点击浏览Button _IEAction($oElement, "click") Return true EndIf Next $oFrames = _IEFrameGetCollection ($oIFrame) $iNumFrames = @extended For $i = 0 to ($iNumFrames - 1) $oFrame = _IEFrameGetCollection ($oIFrame, $i) forEachFrame($oFrame) Next EndFunc
2. SelectFile.au3 功能描述:设置路径,点击打开按钮
#include <IE.au3> If $CmdLine[0]<1 Then Exit EndIf handleupload($CmdLine[1]); ;define a function to handle upload Func handleupload($uploadfile) Dim $title="选择要加载的文件" if WinWait($title,"",4) Then ;wait for window with title attribute for 4 seconds; WinActivate($title) ;active the window; ControlSetText($title,"","Edit1",$uploadfile) ;put the file path into the textfield ControlClick($title,"","Button2") ;click the OK or Save button Else Return False EndIf EndFunc
3. DownloadFile.au3 功能描述:点击保存按钮,设置保存路径,关闭完成对话框
#include <IE.au3> #NoTrayIcon;隐藏托盘图标 If $CmdLine[0]<1 Then Exit EndIf handleDownload($CmdLine[1]) Func handleDownload($SaveAsFileName) Dim $download_title = "文件下载" Dim $save_title = "另存为" Dim $finished_title = "下载完毕" If WinWait($download_title,"",4) Then WinActivate($download_title) Sleep (1000) ControlClick($download_title,"","Button2","") WinWaitActive($save_title,"",4) ControlSetText($save_title,"","Edit1", $saveAsFileName) Sleep (1000) if FileExists ($SaveAsFileName) Then FileDelete($SaveAsFileName) EndIf ControlClick($save_title, "","Button2","") If WinWait($finished_title,"",4) Then ControlClick($finished_title,"","Button4","") Return FileExists($SaveAsFileName) EndIf EndIf Return False EndFunc
4. FileUpload.java 方法调用
/** * <p>Discription:[点击浏览按钮]</p> */ private final String CLICKBROWSE_EXECFILENAME = "clickBrowseBtn.exe"; /** * <p>Discription:[选择文件]</p> */ private final String SELECTFILE_EXECFILENAME = "selectFile.exe"; /** * <p>Discription:[上传文件]</p> * @param filePath * @author:[xxxx] * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述] */ public void uploadFile(String filePath) { //取得当前项目在硬盘上的绝对路径 String basePath = "\""+System.getProperty("user.dir")+"\\exec\\"; try { //打开文件选择窗口 Runtime.getRuntime().exec( basePath + this.CLICKBROWSE_EXECFILENAME + "\""); pause(2000); //选择文件并点击打开按钮 Runtime.getRuntime().exec( basePath + this.SELECTFILE_EXECFILENAME + "\" \""+ filePath + "\"").waitFor(); } catch (Exception e) { e.printStackTrace(); } }
5. FileDownload.java 下载文件
/** * <p>Discription:[下载文件]</p> * @param filePath 文件路径 * @param fileName 文件名 * @author:[xxxx] * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述] */ public void downloadFile(String filePath, String fileName) { try { //将文件保存至指定目录 Runtime.getRuntime().exec( "\""+System.getProperty("user.dir")+"\\exec\\" + this.EXECFILENAME + "\" \""+ filePath+"\\"+fileName + "\"").waitFor(); } catch (InterruptedException e) { log.error("下载文件失败!文件路径:"+filePath+", 文件名:"+fileName); } catch (IOException e) { log.error("下载文件失败!文件路径:"+filePath+", 文件名:"+fileName); } }
参考资料:利用Selenium自动化Web测试
http://www.ibm.com/developerworks/cn/opensource/os-webautoselenium/index.html