zoukankan      html  css  js  c++  java
  • 【Autoit】Autoit 使用

    一、Autoit 上传文件、

    1.常用语法

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

    2.编写脚本

    1)打开网页,点击到上传文件界面。

    如博客园->新随笔->上传图片,停留在此界面不动

    2)打开SciTE Script Editor编写脚本

    WinActivate("打开文件")
    ControlSetText ( "打开文件", "", “Edit1”, "d:1.jpg" )   
    Sleep ( 2000 )  
    ControlClick ( "代开文件", "", “Button1” )  

    编辑完后运行,可看到图片上传成功

    3.元素定位

    打开AutoIt Window Info定位上传文件窗口的元素(title和control id)

    control ID由class和instance组成

    4.导出exe文件

    打开AutoIt Window Info 将脚本导出exe文件

    直接点击exe文件运行或通过cmd运行可看见图片上传成功

    5.python运行

    (参考批量上传图片)

    二、命令行参数上传图片

    通过命令行参数批量上传图片

    1.命令行参数

    参数化传入的参数,可以通过autoit的命令行参数:
    ```
        myProg.exe param1 “This is a string parameter” 99
    ```
    在脚本中,可用以下变量获取命令行参数

    CmdLine[0];=3CmdLine[0];=3CmdLine[1] ; = param1
    CmdLine[2];="Thisisastringparameter"CmdLine[2];="Thisisastringparameter"CmdLine[3] ; = 99
    $CmdLineRaw ; = 'param1 "This is a string parameter" 99'

    CmdLine[0]CmdLine[0]获取的是命令行参数的总数,在上例中CmdLine[0]=3
    CmdLine[1] CmdLine[1] CmdLine[63] 获取的是命令行参数第1到第63位,这个方式最多只能获取63个参数,不过正常情况下是足够用的
    - $CmdLineRaw 获取的是未拆分的所有参数,是一个长字符串,这种情况下不局限与63个参数

    2.将如下代码保存为.exe文件后(文件名随便取一个:cmdjpg.exe),在cmd执行一次,看是否成功
    ```
    WinActivate("文件上传");
    ControlSetText("文件上传", "", "Edit1", $CmdLine[1] );
    Sleep(2000);
    ControlClick("文件上传", "", "Button1");
    ```

    3.用python代码执行
    ```python
    # 需上传图片的路径
    file_path = "D:\1.png"
    # 执行autoit上传文件
    os.system("C:UsersGloriaDesktopcmdjpg.exe %s" % file_path)  # 你自己本地的
    ```

    三、批量上传图片

    1.方法一:先把要上传的图片放到一个list下,然后for循环
    ```python
    # 把需要上传的图片放到一个list下
    all_png = ["D:\1.png", "D:\2.png", "D:\3.png", "D:\4.png"]
    # 循环点击上传图片
    for i in all_png:
        # 1点开编辑器图片

        # 2点开文件上传按钮
        # 执行autoit上传文件

        os.system("C:UsersGloriaDesktopcmdjpg.exe %s" % i)  # 你自己本地的.exe路径
        time.sleep(3)
    ```

    2.方法二:把要上传的图片编号,如:0.png , 1.png, 2.png这种(从0开始编号),放到同一目录下,然后for循环
    ```python
    # 循环点击上传图片
    for i in range(4):
        # 1点开编辑器图片
        # 2点开文件上传按钮
        # 文件名

        file_name = "D:\%s.png" % i  # 参数化路径名称
        # 执行autoit上传文件
        os.system("C:UsersGloriaDesktopcmdjpg.exe %s" % file_name)  # 你自己本地的.exe路径
        time.sleep(3)
    ```

     方法2参考代码:

    from selenium import webdriver
    from selenium.webdriver.support.wait import WebDriverWait
    import os
    import time
    profile_path = r'C:UsersAdministratorAppDataRoamingMozillaFirefoxProfiles2hzyvtjr.default'
    profile = webdriver.FirefoxProfile(profile_path)
    driver = webdriver.Firefox(profile)
    
    def find_element_(loactor):
        e = WebDriverWait(driver,timeout = 30).until(lambda x : x.find_element(*loactor))
        return e
    
    def find_elements_(loactor):
        e = WebDriverWait(driver,timeout = 30).until(lambda x : x.find_elements(*loactor))
        return e
    
    driver.get(r'http://www.cnblogs.com/dhs94/')
    find_element_(('id','blog_nav_newpost')).click()
    #循环上传图片
    for i in range(4):
        find_element_(('id','Editor_Edit_EditorBody_uploadImage')).click()
        iframes = find_elements_(('css selector','iframe'))
        driver.switch_to_frame(iframes[1])
        # find_element_(('name','file')).send_keys(r'C:UserslintaoDesktopme.jpg')
        find_element_(('css selector','.qq-upload-button')).click()
        file_path=r'd:Picturespjy\%s.jpg'%i
        os.system(r'C:UsersAdministratorDesktopss.exe %s'%file_path)
        driver.switch_to_default_content()
        time.sleep(5)
  • 相关阅读:
    [WSUS] Windows Server Update Service 更新后,出现错误不能连接
    [SQLServer] 数据库SA用户被锁定或者忘记密码的恢复
    [SQLServer] 内存占用查看
    [IIS] [PHP] 500.19 随机出现
    [OPENSSL下载][证书] OPENSSL将PFX证书转换为PEM格式
    [VS2015] [asp.net] 允许远程访问本机正在DEBUG的服务
    [VS2008] 安装 AnkhSVN 后,选项中选择它,Pending Changes 窗口一闪而过,源代码管理工具自动跳回 【None】
    [IIS] 配置PHP的过程与坑
    10分钟快速入门vue.js
    前端接口请求的几种方式
  • 原文地址:https://www.cnblogs.com/wsy0202/p/11836924.html
Copyright © 2011-2022 走看看