selenium关于windows弹出框处理以及frame的处理

因为项目需要用到上传文件,就会弹出windows窗口。selenium又没法处理那些窗口。所以只能借助第三方工具对windows窗口操作。

工具:aotuit  首先编辑脚本,然后用其中一个工具将脚本转换成exe文件。

脚本代码如下:

1.Dim $path = "C:\Users\Administrator\Desktop\test.xlsx"
2.Dim $dialogTitle = "文件上传"
3.WinActive($dialogTitle)
4.ControlSetText($dialogTitle,"","Edit1",$path)
5.ControlClick($dialogTitle,"","Button1")

 

说明:

第1,2行代码定义了2个变量,path以及dialogTitle。

第三行代码激活“文件上传”窗口

第四行代码在Edit1下输入path

第五行代码点击Button1按钮。

很简单的代码。

ps:按钮的属性可以用Autoit工具中的windows info找到。

 

代码写完了,保存的时候是*.au3文件。然后用Autoit工具中的Compile Script to .exe工具。将*.au3文件转换成exe文件。

然后我们需要在java下面调用这个exe文件。则用以下代码调用:

try {
Runtime.getRuntime().exec(
"C:\\Users\\Administrator\\Desktop\\test.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

说明:调用C:\\Users\\Administrator\\Desktop\\这个目录下的test.exe文件。

 

 

最后,再来说说对frame的处理。

对frame的处理需要调用一个方法,跳进frame,不然的话定位不到元素。比如我现在有一个frame。id为"test"

代码如下:

driver.switchTo().frame("abc");

说明:这句也就是跳到id为abc的frame。如果有多个frame的话,可以一级一级的往里跳。喔。当然,别忘记跳出,不然没法定位非该frame下的元素。

跳出代码如下:

driver.switchTo().defaultContent();

 

-----记录学习过的东西,忘记了也可以找找。

posted @ 2014-09-02 11:42  Free-program  阅读(1474)  评论(0)    收藏  举报