Autoit 使用
阅读原文时间:2023年07月09日阅读:2

一、Autoit 上传文件、

1.常用语法

- WinActivate("title")         聚焦到指定活动窗口
 - ControlFocus ( "title", "窗口文本", controlID)   设置输入焦点到指定窗口的某个控件上;
 - WinWait ( "title" , "窗口文本" , 超时时间 )  暂停脚本的执行直至指定窗口存在(出现)为止;
 - ControlSetText ( "title", "窗口文本", controlID, "新文本" )   修改控件的文本,如选择文件
 - Sleep ( 延迟 )   使脚本暂停指定时间,单位是毫秒;
 - ControlClick ( "title", "窗口文本", 控件ID , 按钮 , 点击次数 )   向指定控件发送鼠标点击命令;

2.windows窗口定位

  a. 打开 AutoIt Window Info (x64),

  b. 定位工具界面如下:

  

  c. 拖动finder tools到windows 窗口,可定位元素

   在autoit工具的control栏可查看,元素信息

  

3  编写脚本

  打开SciTE Script Editor编写脚本

  

  WinActivate("打开")
  ControlSetText("打开","","Edit1","C:\Users\wang1\Desktop\test.docx")
  Sleep()
  ControlClick("打开","","Button1");

  编辑完后运行(菜单Tools --go),可看到文件上传成功

  注意需要先打开如下图的文件选择框,再运行脚本或exe文件,才能成功

  

4 将脚本导出为.exe文件

  a. 将编写好的脚本保存

  b.  右键脚本,选择 compile Script,即可生成exe文件

    

二、文件上传的参数化

  可通过autoit的命令行参数,实现参数化

aa.exe param1 “This is a string parameter”  666

    aa.exe: 生成的可执行autoit的exe文件

    param1 “This is a string parameter”  666: 传入三个参数,参数1: param1 ,参数2:“This is a string parameter”  ,参数3: 666

  注意:如果传入的参数有空格,则参数必须用双引号括起来

  aa.exe param1 “This is a string parameter”  666:

    $CmdLine[0] : 表示传入的参数的个数(不包括脚本文件名),本例中值为3
    $CmdLine[1] : 表示第 1 个参数,本例中表示 param1
    $CmdLine[2] : 表示第 2 个参数,本例中表示 “This is a string parameter”
    …
    我们常用 $CmdLine[$CmdLine[0]] 来表示最后一个参数…

  除了$CmdLine之外,还有一个变量叫做 $CmdLineRaw,它保存着完整的未被拆分的命令行语句,对于上面这个例子
  $CmdLineRaw 等价于..aa.exe param1 “This is a string parameter”  666

  将以下脚本保存为exe文件,在cmd下执行该文件

 WinActivate("打开")
 ControlSetText("打开","","Edit1", $CmdLine[])
 Sleep(500)
 ControlClick("打开","","Button1");

命令运行前:

命令运行后:

三 python 运行exe文件

def upload\_agreement\_copy(self):  
    """  
    # 这是第二种,通过walk函数,返回root(路径),dirs(子目录),files(文件名)三个元组,本例中我们用walk来遍历  
    返回值:函数返回一个元组,含有三个元素。这三个元素分别是:每次遍历的路径名、路径下子目录列表、目录下文件列表。

    # 先要遍历文件夹  
    path = r'd:\\1'  # 定义路径,OS提供了两种遍历文件夹的办法  
    # files1 = os.listdir(path)  
    # # 这是第一种,会遍历路径下所有文件包括子文件夹,结果以列表给出,但仅仅能给出文件名/子文件名,无法对二者进行区分  
    # # for file in files1:  
    # #     print(os.path.join(path,file))  
    files2 = os.walk(path)  
    """  
    file\_path = self.sign\_contracts.get\_ini\_value("sign\_contracts\_v4.ini", "basic","agreement\_copy\_file")  
    css\_agreement\_copy = self.sign\_contracts.get\_ini\_value("sign\_contracts\_v4.ini", "basic","agreement\_copy")  
    js\_agreement\_copy\_browse = self.sign\_contracts.get\_ini\_value("sign\_contracts\_v4.ini", "basic","js\_agreement\_copy\_browse")

    doc\_list = \[\]  
    for root, dirs, files in os.walk(file\_path):  
        for doc in files:  
            if doc.endswith(".docx") or doc.endswith(".doc"):  
                doc\_file\_path = os.path.join(root, doc)  
                doc\_list.append(doc\_file\_path)  
    choice = random.choice(doc\_list)  
    self.driver.click(css\_agreement\_copy)  
    self.driver.executeJS(js\_agreement\_copy\_browse)  
    time.sleep(1.5)

    #方式一:全路径运行  
    # os.system(r"E:\\HP\\PycharmProjects\\JDMerchant\\config\\upload\_file.exe %s"%choice)

    #方式二  
    exe\_file = self.sign\_contracts\_v4\_config.get\_file\_path("upload\_file.exe")  
    #只有这种试才能运行成功  
    os.system("%s %s"%(exe\_file,choice))  
    log.info("输入的agreement\_copy值为:" + choice)  
    time.sleep(self.time\_wait\_upload)