自动化微博
前提条件
先自己手动登录微博账号
定位app的Package Name 和 Activity Name
adb shell # 进入shell命令中
logcat | grep START # 过滤启动的activity
- package name : com.sina.weibo
- activename : .SplashActivity
启动配置
启动配置
元素定位
使用appium 进行元素定位
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
desired_caps = {
'platformName': 'Android', # 测试Android系统
'platformVersion': '7.1.2', # Android版本 可以在手机的设置中关于手机查看
'deviceName': '127.0.0.1:62001', # adb devices 命令查看 设置为自己的设备
'automationName': 'UiAutomator2', # 自动化引擎
'noReset': True, # 不要重置app的状态
'fullReset': False, # 不要清理app的缓存数据
'appPackage':"com.sina.weibo", # 应用的包名
'appActivity': ".SplashActivity" # 应用的活动页名称
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_capabilities=desired_caps)
import time
# 等待页面加载成功
time.sleep(10)
# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python
el1 = driver.find_element_by_id("com.sina.weibo:id/rightBtn_wrapper")
el1.click()
el2 = driver.find_element_by_accessibility_id("写微博")
el2.click()
# 等待发微博页面加载成功
time.sleep(1)
el3 = driver.find_element_by_id("com.sina.weibo:id/edit_view")
el3.send_keys("Hi 我在使用自动化方式发布微博")
el4 = driver.find_element_by_accessibility_id("发送")
el4.click()
TouchAction(driver).press(x=390, y=200).move_to(x=390, y=1323).release().perform()
# driver.quit()
添加图片
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
desired_caps = {
'platformName': 'Android', # 测试Android系统
'platformVersion': '7.1.2', # Android版本 可以在手机的设置中关于手机查看
'deviceName': '127.0.0.1:62001', # adb devices 命令查看 设置为自己的设备
'automationName': 'UiAutomator2', # 自动化引擎
'noReset': True, # 不要重置app的状态
'fullReset': False, # 不要清理app的缓存数据
'appPackage':"com.sina.weibo", # 应用的包名
'appActivity': ".SplashActivity" # 应用的活动页名称
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_capabilities=desired_caps)
import time
# 等待页面加载成功
time.sleep(10)
# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python
el1 = driver.find_element_by_id("com.sina.weibo:id/rightBtn_wrapper")
el1.click()
time.sleep(1)
el2 = driver.find_element_by_accessibility_id("写微博")
el2.click()
# 等待发微博页面加载成功
time.sleep(1)
el3 = driver.find_element_by_id("com.sina.weibo:id/edit_view")
el3.send_keys("Hi 我在使用自动化方式发布微博")
driver.find_element_by_accessibility_id("插入图片").click()
time.sleep(3)
# 选择图片
xpath_img1 = '//*[@resource-id="com.sina.weibo:id/photo_album_gridview"]/android.widget.RelativeLayout[2]'
xpath_img2 = '//*[@resource-id="com.sina.weibo:id/photo_album_gridview"]/android.widget.RelativeLayout[3]'
driver.find_element_by_xpath(xpath_img1).click()
time.sleep(2)
# 选中
driver.find_element_by_id('com.sina.weibo:id/btn_num_check').click()
# 返回上一层
driver.back()
time.sleep(2)
driver.find_element_by_xpath(xpath_img2).click()
time.sleep(1)
driver.find_element_by_id('com.sina.weibo:id/btn_num_check').click()
# 点击下一步
driver.find_element_by_id('com.sina.weibo:id/btn_confirm_edit').click()
time.sleep(1)
driver.find_element_by_id('com.sina.weibo:id/btn_confirm_edit').click()
time.sleep(1)
el4 = driver.find_element_by_accessibility_id("发送")
el4.click()
TouchAction(driver).press(x=390, y=200).move_to(x=390, y=1323).release().perform()
# driver.quit()