selenium使用autoit3处理windows控件

时间:2022-03-11 03:01:12

selenium本身无法处理windows控件,需要借助autoit
autoit3的语法很简单,如处理上传文件的windows对话框

ControlFocus("请选择上传文件", "","Edit1")
    ControlSetText("请选择上传文件", "", "Edit1", "d:\upload.txt")
    Sleep(2000)
    ControlClick("请选择上传文件", "","Button1");
这些方法语法基本都类似:
第一个参数:title/hwnd/class
ControlClick($hWnd, "", "Edit1")
ControlClick("title", "", "Edit1")
ControlClick("[CLASS:#32770]", "", "Edit1")

controlid :id text,class,classnn,name,instance
如:button1 ;[ID:1003];1003(id),"[CLASSNN:Edit1]", "保存(&s)",[class:button;instance:1],"[NAME:textBoxFolder]",


例如:上面的语句还可以这样写
ControlFocus("[CLASS:#32770]", "","Edit1")
$hwnd=winwait("[CLASS:#32770]","",5)
ControlSetText($hwnd, "", "edit1", $filename)
Sleep(2000)
ControlClick("[CLASS:#32770]", "", "保存(&s)")

如果需要传递参数,则需要封装函数,autoit的参数形式为:$CmdLine[0]为参数个数,$CmdLine[1]表示第一个参数,以此类推,如多个参数,调用时,,中间用空格分割,如upload.exe param1 param2
saveas($CmdLine[1])
Func saveas($filename)
    winactve("[CLASS:#32770]")
    ControlFocus("请选择上传文件", "","Edit1")
    ControlSetText("请选择上传文件", "", "Edit1", $filename)
    Sleep(2000)
    ControlClick("请选择上传文件", "","Button1");
  EndFunc
转换成exe文件,就可以直接调用

java调用:
public static void savefile(String filepath){
  String command = "e:\\upload.exe"+" "+filepath;
  try {
   Runtime.getRuntime().exec(command);
  } catch (IOException e) {
   
   e.printStackTrace();
  }
 }

当然QTP也可以调用:
createobject("wscript.shell").run "d:\upload.exe"

selenium使用autoit3处理windows控件