安卓手机 Python 自动化( uiautomation、uiautomation2、weditor )

发布时间 2023-11-10 15:52:53作者: xiondun

https://blog.csdn.net/freeking101/article/details/105758387

其他自动化工具或者框架:

AutomateIt、Automate、按键精灵、AutoJS 等 Android 自动化工具有什么特点?:https://www.zhihu.com/question/59503646

1、uiautomation

From:https://blog.csdn.net/ma524654165/article/details/77686526

主要用到一个 uiautomation 的开源框架,是一个国人用 Python 封装 Windows GUI (UI Automation) 而成的自动化工具;

开源作者原文:http://www.cnblogs.com/Yinkaisheng/p/3444132.html

Github 地址:https://github.com/yinkaisheng/Python-UIAutomation-for-Windows

此自动化的主要思想:利用此框架抓取到程序的各种句柄,然后对句柄执行各种操作

一、uiautomation 方法

  • 1、WindowContrl(searchDepth,ClassName,SubName)
            查找窗口中的程序,如果有中文则需用Unicode;可用window.Exists(maxSearchSeconds)来判断此窗口是否存在;
  • 2、EditControl(searchFromControl)
            查找编辑位置,找到后可用DoubleClick()来改变电脑的focus;edit.SetValue("string")输入值;
  • 3、Win32API.SendKeys("string") 
            如果已在编辑位置,则可用此方法来输入值,{Ctrl}为ctrl键,其他类似;{@  8}格式可输入8个@,
            对于数字也可实现此功能,但对于字母不能...;
  • 4、MenuItemControl(searchFromControl,Name)                   查找菜单按钮;
  • 5、ComboBoxControl(searchFromControl,AutomationI)      
            查找下拉框,然后在此基础上用Select("name")方法来选择需要的选项;
  • 6、BottonControl(searchFromControl,Name,SubName)        查找按钮;
  • 7、automation.FindControl(firefoxWindow,lambda c:(isinstance(c, automation.EditControl) or isinstance(c, automation.ComboBoxControl)) and c.Name == 'Enter your search term')   按条件搜索handle

二、对找到句柄常用操作

  • Click()            点击;
  • RighClik()       右键点击;
  • SendKeys()     发送字符;
  • SetValue()      传值,一般对EditControl用;

三、对 windows 程序常用操作

  • subprocess.Popen('Name')    用进程打开程序;
  • window.Close()            关闭窗口;
  • window.SetActive()           使用;
  • window.SetTopMost()       设置为顶层
  • window.ShowWindow(uiautomation.ShowWindow.Maximize)  窗口最大化
  • window.CaptureToImage('Notepad.png')  截图;
  • uiautomation.Win32API.PressKey(uiautomation.Keys.VK_CONTROL)    按住Ctrl键
  • uiautomation.Win32API.ReleaseKey(uiautomation.Keys.VK_CONTROL) 释放Ctrl键
  • automation.GetConsoleWindow()      #return console window that runs python,打开控制台
  • automation.Logger.ColorfulWriteLine('\nI will open <Color=Green>Notepad and <Color=Yellow>automate it. Please wait for a while.')  控制台传值(彩色字体),普通传值用WriteLine;
  • automation.ShowDesktop() 显示桌面;

四、句柄的抓取

直接运行 automation 模块枚举窗口时,支持下列参数(从 doc 窗口运行 automation.py 程序 ):

-t intValue     延迟枚举时间,单位秒-r              从树的根部枚举,如果不指定,从当前窗口枚举-d intValue     枚举控件树的的深度,如果不指定,枚举整个树-f              从焦点控件枚举,如果不指定,从当前窗口枚举-c              从光标下的控件枚举,如果不指定,从当前窗口枚举-a              获取光标下控件及其所有父控件-n              显示控件的完整Name, 如果不指定,只显示前30个字符-m              显示控件更多属性,默认只显示控件的四个属性

示例:

automation.pyc –t3,             3秒后枚举当前窗口所有控件automation.pyc –d2 –t3,         3秒后枚举当前窗口前三层控件automation.pyc –r –d1 –t0 -n,   0秒后从根部枚举前两层控件,并显示控件完整名称automation.pyc –c –t3,          3秒后显示鼠标光标下面的控件信息

2、UIAutomator2

参考

uiautomator2 是一个可以使用 Python 对 Android 设备进行UI自动化的库。其底层基于 Google uiautomator,Google 提供的 uiautomator 库可以获取屏幕上任意一个 APP 的任意一个控件属性,并对其进行任意操作。

uiautomator2 不是 android SDK 下的 uiautomator,而是一个 python 库,用于 Android 的 ui 自动化测试。使用 uiautomator2 只能用于 android 端测试,不像 appium 可以跨平台可用于 ios 端。使用 uiautomator2 可以使用 wifi 或数据线和手机相连。

GitHub地址:https://github.com/openatx/uiautomator2
            https://github.com/openatx/uiautomator2/blob/master/README.md

工作原理:

如图所示,python-uiautomator2 主要分为两个部分,python 客户端移动设备

  • Python端:运行脚本,并向移动设备发送 HTTP 请求
  • 移动设备:移动设备上运行了封装了 uiautomator2 的 HTTP 服务,解析收到的请求,并转化成 uiautomator2 的代码。

整个过程

  1. 在移动设备上安装 atx-agent (守护进程), 随后 atx-agent 启动 uiautomator2 服务 ( 默认7912端口 ) 进行监听
  2. 在 PC 上编写测试脚本并执行( 相当于发送 HTTP 请求到移动设备的 server 端)
  3. 移动设备通过 WIFI 或 USB 接收到 PC 上发来的 HTTP 请求,执行制定的操作

安装 uiautomator2

pip install --pre uiautomator2 
pip install pillow (如果需要截图,可安装这个库)

设备安装 atx-agent

首先设备连接到 PC,并能够 adb devices 发现该设备。
执行下面的命令

# init就是所有USB连接电脑的手机上都安装uiautomator2python -m uiautomator2 init # 指定手机安装uiautomator2, 用 --mirrorpython -m uiautomator2 init --mirror --serial $SERIAL # 嫌弃慢的话,可以用国内的镜像python -m uiautomator2 init --mirror

然后就会自动安装库所需要的设备端程序:uiautomator-server,atx-agent,openstf / minicap,openstf / minitouch。 最后提示 success,代表 atx-agent 初始化成功。

安装 weditor

有了这个,方便我们快速的识别手机上的元素,方便写代码

pip install -U weditor

安装好之后,就可以在命令行运行 weditor --help 确认是否安装成功了。Windows 系统可以使用命令在桌面创建一个快捷方式:weditor --shortcut  ,在 windows cmd 中执行上述命令后,会在桌面上创建一个快捷方式,如下图:

安装 weditor 报错 UnicodeDecodeError 时,可以安装老版本:pip install weditor==0.6.3

启动方法

  • 方法 1:命令行执行 weditor 会自动打开浏览器,输入 设备的IP 或者 序列号( 序列号可以通过 adb devices 命令查看得到 ),然后点击 Connect
  • 方法 2:桌面上双击 WEditor 快捷方式即可。
  • 方法 3:命令行中执行 python -m weditor

启动后如下图:

应用以及操作

调用 uiautomator2 的过程

配置手机设备参数,设置具体操作的是哪一台手机
抓取手机上应用的控件,制定对应的控件来进行操作
对抓取到的控件进行操作,比如点击、填写参数等。

设备连接方法,有两种

python-uiautomator2 连接手机的方式有两种,

  • 一种是通过WIFI。WIFI 最便利的地方是可以不用连接数据线
  • 一种是通过USB。USB则可以用在PC和手机网络不在一个网段用不了的情况。

(1)通过WiFi,假设设备IP 192.168.0.107和您的PC在同一网络中

import uiautomator2 as u2d = u2.connect('192.168.0.107') 

(2)通过USB, 假设设备序列是123456789F

import uiautomator2 as u2d = u2.connect('123456789F') # USB链接设备。或者u2.connect_usb('123456f')#d = u2.connect_usb() 或者 d = u2.connect()  ,当前只有一个设备时可以用这个

在没有参数的情况下调用 u2.connect(), uiautomator2 将从环境变量 ANDROID_DEVICE_IP 获取设备 IP。如果这个环境变量是空的,uiautomator 将返回 connect_usb,您需要确保只有一个设备连接到计算机。

检查并维持设备端守护进程处于运行状态:

d.healthcheck()

打开调试开关:

d.debug = Trued.info

安装应用,只能从URL安装:

d.app_install('http://some-domain.com/some.apk')  #引号内为下载apk地址

启动应用:

d.app_start('com.eg.android.AlipayGphone')  #引号内为包名称,这里为支付宝

停止应用:

#相当于'am force-stop'强制停止应用d.app_stop('com.eg.android.AlipayGphone')  #相当于'pm clear' 清空App数据d.app_clear('com.eg.android.AlipayGphone')

停止所有正在运行的应用程序:

# 停止所有d.app_stop_all() # 停止所有应用程序,除了com.examples.demod.app_stop_all(excludes=['com.examples.demo'])

跳过弹窗,禁止弹窗:

d.disable_popups()   # 自动跳过弹出窗口 d.disable_popups(False)  # 禁用自动跳过弹出窗

获取设备信息:

# 获取基本信息d.info # 获取窗口大小print(d.window_size())# 设备垂直输出示例: (1080, 1920)# 设备水平输出示例: (1920, 1080) # 获取当前应用程序信息。对于某些android设备,输出可以为空print(d.current_app()) #获取设备序列号print(d.serial) #获取WIFI IPprint(d.wlan_ip) #获取详细的设备信息print(d.device_info)

获取应用信息:

d.app_info("com.eg.android.AlipayGphone")# 会输出'''{    "packageName": "com.eg.android.AlipayGphone",     "mainActivity": "com.eg.android.AlipayGphone.AlipayLogin",     "label": "支付寶",     "versionName": "10.2.13.9020",     "versionCode": 360,     "size": 108306104}'''# 保存应用程序图标img = d.app_icon("com.eg.android.AlipayGphone")img.save("icon.png")

推拉文件:

(1)将文件推送到设备

# push文件夹d.push("foo.txt", "/sdcard/")# push和重命名d.push("foo.txt", "/sdcard/bar.txt")# push fileobjwith open("foo.txt", 'rb') as f:    d.push(f, "/sdcard/")# 推动和更改文件访问模式d.push("foo.sh", "/data/local/tmp/", mode=0o755)

(2)从设备中拉出一个文件

d.pull("/sdcard/tmp.txt", "tmp.txt") # 如果在设备上找不到文件,FileNotFoundError将引发d.pull("/sdcard/some-file-not-exists.txt", "tmp.txt")

关键事件 ( 屏幕、键盘 操作 ):

(1)打开/关闭屏幕

d.screen_on()#打开屏幕 d.screen_off() #关闭屏幕

(2)获取当前屏幕状态

d.info.get('screenOn')  # 需要 Android> = 4.4

(3)硬键盘和软键盘操作

d.press("home") # 点击home键d.press("back") # 点击back键d.press("left") # 点击左键d.press("right") # 点击右键d.press("up") # 点击上键d.press("down") # 点击下键d.press("center") # 点击选中d.press("menu") # 点击menu按键d.press("search") # 点击搜索按键d.press("enter") # 点击enter键d.press("delete") # 点击删除按键d.press("recent") # 点击近期活动按键d.press("volume_up") # 音量+d.press("volume_down") # 音量-d.press("volume_mute") # 静音d.press("camera") # 相机d.press("power") #电源键

(4)解锁屏幕

d.unlock()# 相当于# 1. 发射活动:com.github.uiautomator.ACTION_IDENTIFY# 2. 按home键

手势与设备的交互:

# 单击屏幕d.click(x,y)  # x,y为点击坐标 # 双击屏幕d.double_click(x,y)d.double_click(x,y,0.1) # 默认两个单击之间间隔时间为0.1秒 # 长按d.long_click(x,y)d.long_click(x,y,0.5)  # 长按0.5秒(默认) # 滑动d.swipe(sx, sy, ex, ey)d.swipe(sx, sy, ex, ey, 0.5) #滑动0.5s(default) #拖动d.drag(sx, sy, ex, ey)d.drag(sx, sy, ex, ey, 0.5)#拖动0.5s(default)# 滑动点 多用于九宫格解锁,提前获取到每个点的相对坐标(这里支持百分比) # 从点(x0, y0)滑到点(x1, y1)再滑到点(x2, y2)# 两点之间的滑动速度是0.2秒d.swipe((x0, y0), (x1, y1), (x2, y2), 0.2)# 注意:单击,滑动,拖动操作支持百分比位置值。例:d.long_click(0.5, 0.5) 表示长按屏幕中心

XPath:

# 检索方向d.orientation# 检索方向。输出可以是 "natural" or "left" or "right" or "upsidedown" # 设置方向d.set_orientation("l") # or "left"d.set_orientation("r") # or "right"d.set_orientation("n") # or "natural" #冻结/ 开启旋转d.freeze_rotation() # 冻结旋转d.freeze_rotation(False) # 开启旋转 ########## 截图 ############# 截图并保存到电脑上的一个文件中,需要Android>=4.2。d.screenshot("home.jpg") # 得到PIL.Image格式的图像. 但你必须先安装pillowimage = d.screenshot() # default format="pillow"image.save("home.jpg") # 或'home.png',目前只支持png 和 jpg格式的图像 # 得到OpenCV的格式图像。当然,你需要numpy和cv2安装第一个import cv2image = d.screenshot(format='opencv')cv2.imwrite('home.jpg', image) # 获取原始JPEG数据imagebin = d.screenshot(format='raw')open("some.jpg", "wb").write(imagebin) ############################# # 转储UI层次结构# get the UI hierarchy dump content (unicoded).(获取UI层次结构转储内容)d.dump_hierarchy()  # 打开通知或快速设置d.open_notification()  #下拉打开通知栏d.open_quick_settings()  #下拉打开快速设置栏 # 检查特定的UI对象是否存在d(text="Settings").exists # 返回布尔值,如果存在则为True,否则为Falsed.exists(text="Settings") # 另一种写法# 高级用法d(text="Settings").exists(timeout=3) # 等待'Settings'在3秒钟出现 # 获取特定UI对象的信息d(text="Settings").info # 获取/设置/清除可编辑字段的文本(例如EditText小部件)d(text="Settings").get_text()  #得到文本小部件d(text="Settings").set_text("My text...")  #设置文本d(text="Settings").clear_text()  #清除文本 # 获取Widget中心点d(text="Settings").center()#d(text="Settings").center(offset=(0, 0)) # 基准位置左前

UI 对象 的 五种 定位方式:

# text、resourceId、description、className、xpath、坐标 # 执行单击UI对象#text定位单击d(text="Settings").click()d(text="Settings", className="android.widget.TextView").click() #resourceId定位单击d(resourceId="com.ruguoapp.jike:id/tv_title", className="android.widget.TextView").click()  #description定位单击d(description="设置").click()d(description="设置", className="android.widget.TextView").click() #className定位单击d(className="android.widget.TextView").click() #xpath定位单击d.xpath("//android.widget.FrameLayout[@index='0']/android.widget.LinearLayout[@index='0']").click() #坐标单击d.click(182, 1264) # 等待元素出现(最多10秒),出现后单击 d(text="Settings").click(timeout=10)# 在10秒时点击,默认的超时0d(text='Skip').click_exists(timeout=10.0)# 单击直到元素消失,返回布尔d(text="Skip").click_gone(maxretry=10, interval=1.0) # maxretry默认值10,interval默认值1.0# 点击基准位置偏移d(text="Settings").click(offset=(0.5, 0.5)) # 点击中心位置,同d(text="Settings").click()d(text="Settings").click(offset=(0, 0)) # 点击左前位置d(text="Settings").click(offset=(1, 1)) # 点击右下 # 执行双击UI对象d(text="设置").double_click() # 双击特定ui对象的中心d.double_click(x, y, 0.1)  # 两次单击之间的默认持续时间为0.1秒 #执行长按UI对象# 长按特定UI对象的中心d(text="Settings").long_click()d.long_click(x, y, 0.5) # 长按坐标位置0.5s默认 # 将UI对象拖向另一个点或另一个UI对象# Android<4.3不能使用drag.# 在0.5秒内将UI对象拖到屏幕点(x, y)d(text="Settings").drag_to(x, y, duration=0.5) # 将UI对象拖到另一个UI对象的中心位置,时间为0.25秒d(text="Settings").drag_to(text="Clock", duration=0.25) 

常见用法:

# 等待10sd.xpath("//android.widget.TextView").wait(10.0) # 找到并单击d.xpath("//*[@content-desc='分享']").click() # 检查是否存在if d.xpath("//android.widget.TextView[contains(@text, 'Se')]").exists:    print("exists")    # 获取所有文本视图文本、属性和中心点for elem in d.xpath("//android.widget.TextView").all():    print("Text:", elem.text)    #获取视图文本for elem in d.xpath("//android.widget.TextView").all():    print("Attrib:", elem.attrib)    #获取属性和中心点#返回: (100, 200)for elem in d.xpath("//android.widget.TextView").all():    print("Position:", elem.center()) # xpath常见用法:# 所有元素//* # resource-id包含login字符//*[contains(@resource-id, 'login')] # 按钮包含账号或帐号//android.widget.Button[contains(@text, '账号') or contains(@text, '帐号')] # 所有ImageView中的第二个(//android.widget.ImageView)[2] # 所有ImageView中的最后一个(//android.widget.ImageView)[last()] # className包含ImageView//*[contains(name(), "ImageView")] 

会在浏览器中打开网页,输入设备 devices 信息 可以 通过 adb devices 来进行查询设备 信息。出现树的模样,代表链接上手机,然后点击右上角的 Reload 实时显示手机页面。 这样就可以开始你的元素定位了。

通用的元素定位方式。

  • (1) 根据文本进行定位:d(text=显示的文本).click()

  • (2) 通过 resourceId 进行定位:d(resourceId="com.tcl.tclplus:id/cart_layout").click()

  • (3) 滑动 上 或者 下 。手指向上,就是页面往下拉,分两种情况 拉到底 或者 只拉一部分。

    • 拉到底:d(scrollable=True).scroll.toEnd()
    • 拉一部分:d.swipe(0.806, 0.801,0.818, 0.487) # 向上滑动  横坐标可以不变,纵坐标是变化的,是变小的趋势  这是手指向上
    • 拉倒页面首页 开头部分:d(scrollable=True).scroll.toBeginning(steps=50)
  • (4) 滑动 左右 
            d(scrollable=True).scroll.horiz.toEnd() #横向滚动到最右侧
            d(scrollable=True).scroll.horiz.toBeginning() #横向滚动到最左侧
        或者
            c.水平向右滚动:d(scrollable=True).scroll.horiz.forward(steps=50)
            d.水平向左滚动:d(scrollable=True).scroll.horiz.backward(steps=50)

  • (5) 滑动到指定位置:滑动到文本为测试的位置:d(scrollable=True).scroll.to(text ='测试')

  • (6) 元素判断  可以这样写,通过判断元素的存在性来执行不同的操作

    s = self.d(    resourceId="com.tcl.tclplus:id/iot_txt_home_name",     text=u"立即登录", className="android.widget.TextView")if len(s) == 0:    print('元素未找到,执行退出操作')
    
  • (7) 随机字符串或者随机字母 输入

    a = random.sample(string.ascii_letters, 4)data = ''.join([str(x) for x in a])# 随机从大小写字母中取四位,然后写入到输入框中。d(resourceId="com.tcl.tclplus:id/et_invoice_header").set_text(data)
    
  • (8) 截图

    filepaths 就是截图所存的路径,可以自己填写。 filepaths = os.path.normpath(os.path.join(os.path.join(os.path.dirname(os.path.dirname(__file__)), "Automation/Tcase")))def get_png(self, filename, filepath='/Member'):    """    截图操作,默认截图的存储路径为Member    """    imgName = filename + datetime.datetime.now().strftime('%Y%m%d%H%M%S') + '.png'  # 截图可以单独保存为别的名字的文件    stringPath = filepaths + '/image' + filepath + '\\' + imgName    print stringPath    # img.save('filename.png')#图片保存在当前项目的文件夹下边    self.d.screenshot().save(stringPath)  # 保存文件到指定的文件夹下边 self.get_png(filename='订单-取消购买'.decode('utf-8'), filepath='/Market/订单')  实际调用的时候 需要进行decode
    
  • (9) apk 自动安装

    def install_app(path):    app_Path = lambda x: os.path.join(file_path, "app", x)    apk = app_Path(path + ".apk")    os.system('adb install -r -d ' + apk)    time.sleep(5)
    
  • (10) 报告生成

    # 用例文件很多 采用 关键字匹配的方式进行。 def UI_Report(testcase, ReportPath):    """    根据传入的testcase 来判断执行哪种报告生成方式。    """     def Report(s):        @wraps(s)        def creat_report():            AA = hasattr(testcase,'__call__')            # AA = isfunction(testcase)            if AA:                print '这个是函数'                suite = unittest.makeSuite(testcase)                fp = file(ReportPath, 'wb')                runner = HTMLTestRunner(stream=fp, title=r'UI自动化', description=r'接口自动化测试报告')                runner.run(suite) #区别                fp.close()            else:                print '不是函数,是执行run_main方法'                fp = file(ReportPath, 'wb')                runner = HTMLTestRunner(stream=fp, title=r'UI自动化测试报告', description=r'商城')                runner.run(testcase)                fp.close()        return creat_report    return Report  def all_case():    testcase = unittest.TestSuite()  # 加载测试套件    # 用例的目录,关键字进行匹配    discover = unittest.defaultTestLoader.discover(filepath, pattern='Market*.py', top_level_dir=None)    testcase.addTest(discover)    return testcase  @UI_Report(testcase=all_case(),ReportPath=filepath+'/report/result.html')def run():    print '生成测试报告'  if __name__ == '__main__':    run()
    

    因为 run_main 执行的时候,是打印所有用例文件+名称,而通过测试套件 添加 类名称时,执行方式不一样。至于公共函数,比如启动 app、一些共有的操作,可以写到 公共函数中

    在unitest中 增加setupclass 函数 是只 启动一次app,跟setup 区别一下,下面增加你的公共函数。然后用例文件中 直接通过继承的方式来进行。

例如:class test(commons):

def del_file(filepath):    listdir = os.listdir(filepath)  # 获取文件和子文件夹    for rename in listdir:        rename = filepath + "//" + rename        if os.path.isfile(rename):  # 是文件            os.remove(rename)  # 删除文件        elif os.path.isdir(rename):  # 是子文件            duellist = os.listdir(rename)            for f in duellist:  # 遍历该子文件夹                file_path = os.path.join(rename, f)                if os.path.isfile(file_path):  # 删除子文件夹下文件                    os.remove(file_path)                elif os.path.isdir(file_path):  # 强制删除子文件夹下的子文件夹                    shutil.rmtree(file_path)

可以在用例文件执行开头增加一些 调用方法

如删除截图 和初始化 app环境 都是可以的

3、pyautogui

From:https://www.jb51.net/article/183926.htm

在使用 Python 做 安卓自动化脚本 时,两个库可以使用,一个为 PyUserInput 库,另一个为pyautogui 库。就本人而言,我更喜欢使用pyautogui库,该库功能多,使用便利。下面给大家介绍一下pyautogui库的使用方法。在cmd命令框中输入pip3 install pyautogui即可安装该库!

常用操作

我们在 pyautogui 库中常常使用的方法,如下:

import pyautogui # 调用在执行动作后暂停的秒数,只能在执行一些pyautogui动作后才能使用,建议用time.sleeppyautogui.PAUSE = 1 # 启用自动防故障功能,左上角的坐标为(0,0),将鼠标移到屏幕的左上角,来抛出failSafeException异常pyautogui.FAILSAFE = True # 判断(x,y)是否在屏幕上x, y = 122, 244pyautogui.onScreen(x, y)  # 结果为true width, height = pyautogui.size()  # 屏幕的宽度和高度print(width, height)

鼠标操作

我们在 pyautogui 库对于鼠标的使用方法大体如下:

import pyautogui currentMouseX, currentMouseY = pyautogui.position()  # 鼠标当前位置print(currentMouseX, currentMouseY) # 控制鼠标移动,duration为持续时间for i in range(2):    pyautogui.moveTo(100, 100, duration=0.25)  # 移动到 (100,100)    pyautogui.moveTo(200, 100, duration=0.25)    pyautogui.moveTo(200, 200, duration=0.25)    pyautogui.moveTo(100, 200, duration=0.25) for i in range(2):    pyautogui.moveRel(50, 0, duration=0.25)  # 从当前位置右移100像素    pyautogui.moveRel(0, 50, duration=0.25)  # 向下    pyautogui.moveRel(-50, 0, duration=0.25)  # 向左    pyautogui.moveRel(0, -50, duration=0.25)  # 向上 # 按住鼠标左键,把鼠标拖拽到(100, 200)位置pyautogui.dragTo(100, 200, button='left')# 按住鼠标左键,用2秒钟把鼠标拖拽到(300, 400)位置pyautogui.dragTo(300, 400, 2, button='left')# 按住鼠标左键,用0.2秒钟把鼠标向上拖拽pyautogui.dragRel(0, -60, duration=0.2) # pyautogui.click(#     x=moveToX, y=moveToY, clicks=num_of_clicks, #     interval=secs_between_clicks, button='left'# )# 其中,button属性可以设置成left,middle和right。pyautogui.click(10, 20, 2, 0.25, button='left')pyautogui.click(x=100, y=200, duration=2)  # 先移动到(100, 200)再单击pyautogui.click()  # 鼠标当前位置点击一下pyautogui.doubleClick()  # 鼠标当前位置左击两下pyautogui.doubleClick(x=100, y=150, button="left")  # 鼠标在(100,150)位置左击两下pyautogui.tripleClick()  # 鼠标当前位置左击三下 pyautogui.mouseDown()  # 鼠标左键按下再松开pyautogui.mouseUp()pyautogui.mouseDown(button='right')  # 按下鼠标右键pyautogui.mouseUp(button='right', x=100, y=200)  # 移动到(100, 200)位置,然后松开鼠标右键 # scroll函数控制鼠标滚轮的滚动,amount_to_scroll参数表示滚动的格数。正数则页面向上滚动,负数则向下滚动# pyautogui.scroll(clicks=amount_to_scroll, x=moveToX, y=moveToY)pyautogui.scroll(5, 20, 2)pyautogui.scroll(10)  # 向上滚动10格pyautogui.scroll(-10)  # 向下滚动10格pyautogui.scroll(10, x=100, y=100)  # 移动到(100, 100)位置再向上滚动10格 # 缓动/渐变函数可以改变光标移动过程的速度和方向。通常鼠标是匀速直线运动,这就是线性缓动/渐变函数。# PyAutoGUI有30种缓动/渐变函数,可以通过pyautogui.ease*?查看。# 开始很慢,不断加速pyautogui.moveTo(100, 100, 2, pyautogui.easeInQuad)# 开始很快,不断减速pyautogui.moveTo(100, 100, 2, pyautogui.easeOutQuad)# 开始和结束都快,中间比较慢pyautogui.moveTo(100, 100, 2, pyautogui.easeInOutQuad)# 一步一徘徊前进pyautogui.moveTo(100, 100, 2, pyautogui.easeInBounce)# 徘徊幅度更大,甚至超过起点和终点pyautogui.moveTo(100, 100, 2, pyautogui.easeInElastic)

对于我们要获取鼠标在屏幕中的位置,我们可以采用如下代码:

# 案例获取鼠标的位置,方便复制我们定位的鼠标坐标点到代码中import pyautoguiimport time  # 获取鼠标位置def get_mouse_position():    time.sleep(5)  # 准备时间    print('开始获取鼠标位置')    try:        for i in range(10):            # Get and print the mouse coordinates.            x, y = pyautogui.position()            positionStr = '鼠标坐标点(X,Y)为:{},{}'.format(str(x).rjust(4), str(y).rjust(4))            pix = pyautogui.screenshot().getpixel((x, y))  # 获取鼠标所在屏幕点的RGB颜色            positionStr += ' RGB:(' + str(pix[0]).rjust(3) + ',' + str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(                3) + ')'            print(positionStr)            time.sleep(0.5)  # 停顿时间    except:        print('获取鼠标位置失败')  if __name__ == "__main__":    get_mouse_position()

也可以使用pyautogui库帮助文档的方法,不过本人认为使用上面的方法,更加便利。pyautogui库帮助文档的获取鼠标位置的方法如下:

import pyautogui print('Press Ctrl-C to quit.')try:    while True:        # Get and print the mouse coordinates.        x, y = pyautogui.position()        positionStr = 'X:' + str(x).rjust(4) + ' Y:' + str(y).rjust(4)        pix = pyautogui.screenshot().getpixel((x, y))  # 获取鼠标所在屏幕点的RGB颜色        positionStr += ' RGB:(' + str(pix[0]).rjust(3) + ',' + \                       str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(3) + ')'        print(positionStr, end='')  # end='' 替换了默认的换行         # 连续退格键并刷新,删除之前打印的坐标,就像直接更新坐标效果        print('\b' * len(positionStr), end='', flush=True)  except KeyboardInterrupt:  # 处理 Ctrl-C 按键    print('\nDone.')

键盘操作

我们在pyautogui库对于键盘的使用方法大体如下:

import pyautogui pyautogui.typewrite('Hello world!')  # 输入Hello world!字符串pyautogui.typewrite('Hello world!', interval=0.25)  # 每次输入间隔0.25秒,输入Hello world! pyautogui.press('enter')  # 按下并松开(轻敲)回车键pyautogui.press(['left', 'left', 'left', 'left'])  # 按下并松开(轻敲)四下左方向键pyautogui.keyDown('shift')  # 按下`shift`键pyautogui.keyUp('shift')  # 松开`shift`键 pyautogui.keyDown('shift')pyautogui.press('4')pyautogui.keyUp('shift')  # 输出 $ 符号的按键 pyautogui.hotkey('ctrl', 'v')  # 组合按键(Ctrl+V),粘贴功能,按下并松开'ctrl'和'v'按键 # pyautogui.KEYBOARD_KEYS数组中就是press(),keyDown(),keyUp()和hotkey()函数可以输入的按键名称pyautogui.KEYBOARD_KEYS = [    '\t', '\n', '\r', ' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.',    '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@',    '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',    'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',    'accept', 'add', 'alt', 'altleft', 'altright', 'apps', 'backspace', 'browserback',    'browserfavorites', 'browserforward', 'browserhome', 'browserrefresh', 'browsersearch',    'browserstop', 'capslock', 'clear', 'convert', 'ctrl', 'ctrlleft', 'ctrlright', 'decimal',    'del', 'delete', 'divide', 'down', 'end', 'enter', 'esc', 'escape', 'execute', 'f1', 'f10',    'f11', 'f12', 'f13', 'f14', 'f15', 'f16', 'f17', 'f18', 'f19', 'f2', 'f20', 'f21', 'f22',    'f23', 'f24', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'final', 'fn', 'hanguel', 'hangul',    'hanja', 'help', 'home', 'insert', 'junja', 'kana', 'kanji', 'launchapp1', 'launchapp2',    'launchmail', 'launchmediaselect', 'left', 'modechange', 'multiply', 'nexttrack',    'nonconvert', 'num0', 'num1', 'num2', 'num3', 'num4', 'num5', 'num6', 'num7', 'num8', 'num9',    'numlock', 'pagedown', 'pageup', 'pause', 'pgdn', 'pgup', 'playpause', 'prevtrack', 'print',    'printscreen', 'prntscrn', 'prtsc', 'prtscr', 'return', 'right', 'scrolllock', 'select',    'separator', 'shift', 'shiftleft', 'shiftright', 'sleep', 'space', 'stop', 'subtract', 'tab',    'up', 'volumedown', 'volumemute', 'volumeup', 'win', 'winleft', 'winright', 'yen', 'command',    'option', 'optionleft', 'optionright']

弹窗操作

我们在 pyautogui 库对于弹出窗口的使用方法大体如下:

import pyautogui # 显示一个简单的带文字和OK按钮的消息弹窗。用户点击后返回button的文字。pyautogui.alert(text='', title='', button='OK')b = pyautogui.alert(text='要开始程序么?', title='请求框', button='OK')print(b)  # 输出结果为OK # 显示一个简单的带文字、OK和Cancel按钮的消息弹窗,用户点击后返回被点击button的文字,支持自定义数字、文字的列表。pyautogui.confirm(text='', title='', buttons=['OK', 'Cancel'])  # OK和Cancel按钮的消息弹窗pyautogui.confirm(text='', title='', buttons=range(10))  # 10个按键0-9的消息弹窗a = pyautogui.confirm(text='', title='', buttons=range(10))print(a)  # 输出结果为你选的数字 # 可以输入的消息弹窗,带OK和Cancel按钮。用户点击OK按钮返回输入的文字,点击Cancel按钮返回None。pyautogui.prompt(text='', title='', default='') # 样式同prompt(),用于输入密码,消息用*表示。带OK和Cancel按钮。# 用户点击OK按钮返回输入的文字,点击Cancel按钮返回None。pyautogui.password(text='', title='', default='', mask='*')

图像操作

我们在pyautogui库对于图像的使用方法大体如下:

import pyautogui # 截全屏并设置保存图片的位置和名称pyautogui.screenshot(r'C:\Users\ZDH\Desktop\PY\my_screenshot.png')   # 截全屏并设置保存图片的位置和名称im = pyautogui.screenshot(r'C:\Users\ZDH\Desktop\PY\my_screenshot.png')  print(im)  # 打印图片的属性 # 不截全屏,截取区域图片。截取区域region参数为:左上角XY坐标值、宽度和高度pyautogui.screenshot(    r'C:\Users\ZDH\Desktop\PY\region_screenshot.png',     region=(0, 0, 300, 400)) pix = pyautogui.screenshot().getpixel((220, 200))  # 获取坐标(220,200)所在屏幕点的RGB颜色positionStr = ' RGB:(' + str(pix[0]).rjust(3) + ',' + \              str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(3) + ')'print(positionStr)  # 打印结果为RGB:( 60, 63, 65)pix = pyautogui.pixel(220, 200)  # 获取坐标(220,200)所在屏幕点的RGB颜色与上面三行代码作用一样positionStr = ' RGB:(' + str(pix[0]).rjust(3) + ',' + \              str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(3) + ')'print(positionStr)  # 打印结果为RGB:( 60, 63, 65) # 如果你只是要检验一下指定位置的像素值,可以用pixelMatchesColor(x,y,RGB)函数,把X、Y和RGB元组值穿入即可# 如果所在屏幕中(x,y)点的实际RGB三色与函数中的RGB一样就会返回True,否则返回False# tolerance参数可以指定红、绿、蓝3种颜色误差范围pyautogui.pixelMatchesColor(100, 200, (255, 255, 255))pyautogui.pixelMatchesColor(100, 200, (255, 255, 245), tolerance=10) # 获得文件图片在现在的屏幕上面的坐标,返回的是一个元组(top, left, width, height)# 如果截图没找到,pyautogui.locateOnScreen()函数返回Nonea = pyautogui.locateOnScreen(r'C:\Users\ZDH\Desktop\PY\region_screenshot.png')print(a)  # 打印结果为Box(left=0, top=0, width=300, height=400)x, y = pyautogui.center(a)  # 获得文件图片在现在的屏幕上面的中心坐标print(x, y)  # 打印结果为150 200 # 这步与上面的四行代码作用一样x, y = pyautogui.locateCenterOnScreen(r'C:\Users\ZDH\Desktop\PY\region_screenshot.png')  print(x, y)  # 打印结果为150 200 # 匹配屏幕所有与目标图片的对象,可以用for循环和list()输出pyautogui.locateAllOnScreen(r'C:\Users\ZDH\Desktop\PY\region_screenshot.png')for pos in pyautogui.locateAllOnScreen(r'C:\Users\ZDH\Desktop\PY\region_screenshot.png'):    print(pos)# 打印结果为Box(left=0, top=0, width=300, height=400)a = list(pyautogui.locateAllOnScreen(r'C:\Users\ZDH\Desktop\PY\region_screenshot.png'))print(a)  # 打印结果为[Box(left=0, top=0, width=300, height=400)]

参考资料

PyAutoGUI帮助文档:
https://blog.csdn.net/qq_34053552/article/details/79776671
pyautogui图形自动化,击败重复性办公任务:
https://blog.csdn.net/qq_43017750/article/details/90575240