bling123 发表于 2019-4-26 21:55:04

python+uiautomator2 实现需求:从通讯录添加手机号码,若存在旧的 SOS号码,先删...

#   实现需求:从通讯录添加手机号码,若存在旧的SOS号码,先删除再添加,若通讯录中没有号码,需先新建。

import uiautomator2 as u2
from time import sleep
import time
import random

distinct_devices ="H6ZPKR45DAB64DJB"
device = u2.connect(distinct_devices)
package = "com.njzx.shbsetting"
path = "E:\\pycharm\\run\\UiAutomator2\\F5\\SOS\\FromBookPic\\"
path_result = "E:\\pycharm\\run\\UiAutomator2\\F5\\SOS\\result\\"
now_time = time.strftime("%Y-%m-%d-%H-%M-%S")

#   启动SOS应用
def start_app():
    device.app_start(package)
    sleep(1)

#从通讯录添加号码并保存
def addTel_submit():
    device(resourceId="com.njzx.shbsetting:id/layout_sos").click()
    sleep(1)
    #   判断是否存在旧的SOS号码,若存在先删除再添加
    count_elments = device(resourceId = "zte.shb.sossetting:id/number").count
    print("已有的旧的SOS号码个数为:"+ str(count_elments) + "。\n稍后执行删除动作!")
    if count_elments != 0:
      for i in range(0,count_elments):
            device(resourceId="zte.shb.sossetting:id/delete").click()
            device(resourceId="zte.shb.sossetting:id/btn_set").click()
            print("删除成功!")
      getFromContactors()
      device.press("home")
    else:
      getFromContactors()
      device.press("home")


#   从通讯录选择号码:
list_name = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n"]
list_tel = ["1234","143536","5656565435","2344567","234455756","686778967","144354657",
            "562789","90909090","6725111","6764535"]
def getFromContactors():
    device(resourceId = "zte.shb.sossetting:id/add_from_contactors").click()
    sleep(1)
    #   判断有没有联系人,如果没有,返回到联系人应用先新建联系人 package = "com.android.dialer",然后添加
    none_elements = device(text = "您还没有添加任何联系人")
    dialer_package = "com.android.dialer"
    if none_elements.exists:
      device.app_start(dialer_package)
      device(text = "通讯录").click()#   确保在联系人界面
      sleep(1)
      #   判断列表中:姓名列表的长度和号码列表的长度和最多能够添加多少个SOS号码(也就是5)的大小,然后新建联系人
      if len(list_name) >= 5 and len(list_tel)>=5 :
            for i in range(0,5):
                print("直接选取5个号码")
                addNameAddTel()
      elif len(list_name) < 5 and len(list_tel) <= len(list_name):
            for i in range(0,len(list_tel)):    #   len(list_tel)
                print("以list_tel的长度作为全部号码")
                addNameAddTel()
      elif len(list_tel) < 5 and len(list_name) <= len(list_tel):
            for i in range(0,len(list_name)):    #   len(list_name)
                print("以list_name的长度作为全部号码")
                addNameAddTel()
      elif len(list_name) >5 and len(list_tel)<5:
            for i in range(0,len(list_tel)):    #   len(list_tel)
                print("以list_tel的长度作全部号码")
                addNameAddTel()
      elif len(list_tel)>5 and len(list_name)<5:
            for i in range(0,len(list_name)):    #   len(list_name)
                print("以list_name的长度作为全部号码")
                addNameAddTel()
      #   重新打开SOS,选择已经新建好的号码:
      start_app()
      device(resourceId="com.njzx.shbsetting:id/layout_sos").click()
      sleep(1)
      device(resourceId="zte.shb.sossetting:id/add_from_contactors").click()
      sleep(1)
      image = device.screenshot()
      image.save(path + now_time + '.jpg')
      sleep(1)
      selectTelFromContect()
      sleep(2)
      image = device.screenshot()
      image.save(path_result + now_time + '.jpg')
    #   若存在联系人,直接添加
    else:
      selectTelFromContect()
      sleep(2)
      image = device.screenshot()
      image.save(path_result + now_time + '.jpg')

#   向联系人中新建号码
def addNameAddTel():
    device(resourceId="com.android.dialer:id/zte_edit_contact").click()
    device(text="添加联系人").click()
    sleep(1)
    select_name = random.choice(list_name)# 随机选取一个姓名
    list_name.remove(select_name)# 姓名不重复
    print("本次随机选择的姓名是:" + select_name + "。\n稍后删除" + select_name + ",打印出剩余姓名!")
    print(list_name)
    device(text="姓名").click()
    device(text="姓名").send_keys(select_name)

    select_tel = random.choice(list_tel)
    list_tel.remove(select_tel)# 号码不重复
    print("本次随机选择的号码是:" + select_tel + "。\n稍后删除" + select_tel + ",打印出剩余号码!")
    print(list_tel)
    device(text="电话").click()
    device(text="电话").send_keys(select_tel)
    device(text="完成").click()
    sleep(1)
    device.press("back")
    sleep(1)


sostel1 = device(resourceId = "com.android.contacts:id/cliv_name_textview",instance=0)
sostel2 = device(resourceId = "com.android.contacts:id/cliv_name_textview",instance=1)
sostel3 = device(resourceId = "com.android.contacts:id/cliv_name_textview",instance=2)
sostel4 = device(resourceId = "com.android.contacts:id/cliv_name_textview",instance=3)
sostel5 = device(resourceId = "com.android.contacts:id/cliv_name_textview",instance=4)
list_sosTel =
#   从联系人中随机选择SOS号码并添加
def selectTelFromContect():
    for i in range(0,5):
      selectFromsosTel = random.choice(list_sosTel)
      selectFromsosTel.click()
      list_sosTel.remove(selectFromsosTel)
      print(list_sosTel)
      device(text = "添加").click()
      device(text = "从通讯录中添加").click()



if __name__ == '__main__':
    start_app()
    addTel_submit()

Miss_love 发表于 2020-12-30 16:00:35

支持
页: [1]
查看完整版本: python+uiautomator2 实现需求:从通讯录添加手机号码,若存在旧的 SOS号码,先删...