Selenium 处理安全对话框 (windows security dialog)用autoit 来代替
如下图这种系统弹出的验证窗口,selenium处理不了,所以我们可以借助外部工具来实现。
http://autoit-cn.googlecode.com/svn/trunk
Autoit小巧而且使用简单。上面是autoit的SVN地址,用svn Checkout,查看AutoIt.chm 文档,就可以很容易上手。感谢autoit汉化组的同学们,做的仔细的翻译和工具整理,在编辑器提示参数都是中文的。
- Dim $account = $CmdLine[1]
- Dim $password = $CmdLine[2]
- Dim $dialogTitle = "Windows Security"
- WinActivate($dialogTitle)
- WinWaitActive($dialogTitle)
- Sleep(1*1000)
- ControlSetText($dialogTitle,"","Edit1",$account)
- Sleep(1*1000)
- ControlSetText($dialogTitle,"","Edit2",$password)
- ControlClick($dialogTitle,"","Button2")
- Sleep(1*1000)
上面一段实现跳过安全对话框的autoit脚本。
$CmdLine[1]和CmdLine[2] 代表命令行的第一个参数和第二参数。
WinActivate($dialogTitle)激活标题为”WindowsSecurity” 窗口。
ControlSetText($dialogTitle,"","Edit1",$account)向username 编辑框输入用户名
ControlSetText($dialogTitle,"","Edit2",$password)向password编辑框输入密码
ControlClick($dialogTitle,"","Button2")点击ok按钮
将上面的autoit脚本编译为 dealSecurityDialog.exe 文件
下面写一段处理java 代码来调用dealSecurityDialog.exe,并传入对应帐户和密码。
- public String dealSecurityDialog(String admin,String password){
- String return_str = "true";
- if(admin == null || admin.equals("")){
- logger.error("admin is empty");
- return_str = "false";
- }else if(password == null || admin.equals("")){
- logger.error("password is empty");
- return_str = "false";
- }else{
- Environment env = Environment.getInstance();
- String safxDir = env.getSafxDir();
- String dealSecurityDialogExe = safxDir + "/utils/dealSecurityDialog.exe " + admin + " " + password;;
-
-
- dealSecurityDialogExe = dealSecurityDialogExe.replace("/", "\\");
- Runtime rt = Runtime.getRuntime();
- Process p;
- try {
- p = rt.exec(dealSecurityDialogExe);
- BufferedInputStream in = new BufferedInputStream(p.getInputStream());
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- String s;
- while ((s = br.readLine()) != null){
- logger.info(s);
- }
- } catch (IOException e) {
- logger.error(e.getMessage());
- }
- }
- return return_str;
- }
下面这个字串,定义了dealSecurityDialog。Exe的目录,我方的目录在 utils文件下下。
StringdealSecurityDialogExe = safxDir + "/utils/dealSecurityDialog.exe"+ admin + " " + password;;
只要在相应的代码里调用dealSecurityDialog方法,就是可以实现跳过安全对话框的操作。