第一个appium项目,找到企业微信自己每日的日报保存到表格里
1.按照之前文章,python链接启动appium,启动“雷电”模拟器
2.在模拟器下载 企业微信,并成功登陆一次
3.按照之前文章找到我们需要的 deviceName 、appPackage与 appActivity,并填写好
4.剩下循环找到日报内容及保存到表格代码,如下
import xlwt from appium import webdriver import time # server 启动参数 desired_caps = {}
# 保存上次登陆记录,不需要再输入密码 desired_caps['noReset'] = 'True' desired_caps['fullReset'] = 'False'
# 平台的名称 desired_caps['platformName'] = 'android' desired_caps['platformVersion'] = '5.1.1' desired_caps['deviceName'] = '127.0.0.1:5555' desired_caps['appPackage'] = 'com.tencent.wework' desired_caps['appActivity'] = 'com.tencent.wework.launch.WwMainActivity' # unicode设置(允许中文输入) desired_caps['unicodeKeyboard'] = True # 键盘设置(允许中文输入) desired_caps['resetKeyboard'] = True # 声明对象后会直接启动参数中的应用 driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) time.sleep(6)
# 获取元素找到我们需要的记录 dict_info = {} driver.find_element_by_xpath("//*[contains(@text,'汇报')]").click() driver.find_element_by_id('com.tencent.wework:id/h9p').click() driver.find_element_by_xpath("//*[contains(@text,'进入应用')]").click() super_element = driver.find_element_by_id("com.tencent.wework:id/a_z") super_element.find_element_by_xpath("//*[contains(@text,'记录')]").click() time.sleep(2)
# 循环找到当前最近4次的日报内容,保存到 dict_info 字典里 for i in range(4): driver.find_element_by_xpath("//*[contains(@text,'今日工作内容')]").click() time.sleep(6) # 选择元素时可选择class类元素的第几个第几个来依次选择 date = driver.find_elements_by_class_name("android.view.View")[0].get_attribute("name") dict_info["%s 日期" % date] = date t1 = driver.find_elements_by_class_name("android.view.View")[3].get_attribute("name") dict_info['%s 今日工作内容' % date] = t1 t2 = driver.find_elements_by_class_name("android.view.View")[5].get_attribute("name") dict_info['%s 今日成果总结' % date] = t2 t3 = driver.find_elements_by_class_name("android.view.View")[7].get_attribute("name") dict_info['%s 明日计划' % date] = t3 time.sleep(6)
# 取完值,点击 返回上一页 driver.find_element_by_id('com.tencent.wework:id/h9e').click()
# 向上滑动到另一个今日工作计划内容的位置 driver.swipe(700, 815, 500, 570)
driver.quit() f = xlwt.Workbook()
#创建工作簿,表格的名字为sheet1 sheet1 = f.add_sheet(u'sheet1', cell_overwrite_ok=True) data_list = [] for k, v in dict_info.items(): # 循环取出字典的value值 data_list.append(v) # 每4个信息组成一个新的列表 new_list = [data_list[i:i + 4] for i in range(0, len(data_list), 4)] # 表格头展示 header = ['日期', '今日工作内容', '今日成果总结', '明日计划'] first_col = 0 for i in header: col = header.index(i) sheet1.write(first_col, col, i) i = 1 for one_data in new_list: j = 0 for data in one_data: sheet1.write(i, j, data) j += 1 i += 1 # 保存到data.xlsx这个表格里 f.save('data.xlsx') print("表格录入成功!")