1、第一步
微信内H5页面调试需要将调试模式打开,微信任意聊天框http://debugmm.qq.com/?forcex5=true
http://debugx5.qq.com
点击信息,勾选打开调试功能
2、第二步
下载谷歌对应的驱动,打开chrome浏览器,地址栏输入 chrome://inspect ,出现如下所示界面
https://chromedriver.storage.googleapis.com/index.html,浏览器驱动下载地址。下载了对应的驱动后,以前的驱动可以新建一个文件夹放着,然后再到appium里面去填写驱动的路径。
小程序只能用xpath定位!!!
3、第三步
有h5锲套的APP或者小程序,要下载uc开发者工具,直接百度即可。
h5锲套的直接用uc开发者工具进行元素定位即可。直接右键复制xpath定位。跟浏览器上的f12一样。
from appium import webdriver
from time import sleep as s
from Testcase.homepage import HomePage
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
# from selenium.common.exceptions import NoSuchElementException
#手机的信息
desired_caps = {
"devicesName": "FFK0217724002496",
"platformName": "Android",
"platformVersion": "9",
"appPackage": "com.tencent.mm",
"appActivity": ".ui.LauncherUI",
'newCommandTimeout': 6000,
"noReset": True,
"unicodeKeyboard": True,
"resetKeyboard": True,
"automationName": "uiautomator2",
"chromeOptions": {"androidProcess": "com.tencent.mm:appbrand0"},
# "showChromedriverLog": True,
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.implicitly_wait(8)
# 等待activity出现后每两秒查找一次
# driver.wait_activity("",100,interval=2)
# AC = driver.current_activity
# print(AC)
#点击微信
driver.find_elements_by_id('com.tencent.mm:id/dtx')[2].click()
sleep(3)
driver.find_elements_by_id('com.tencent.mm:id/dvz')[6].click()
sleep(3)
driver.find_elements_by_id('com.tencent.mm:id/ik3')[0].click()
sleep(3)
conss = driver.contexts
print('当前小程序所有的上下文:{}'.format(conss))
#切换窗口
print('开始切换上下文')
driver.switch_to.context('WEBVIEW_com.tencent.mm:appbrand0')
print('切换成功')
hs = driver.current_window_handle
print('当前所有的窗口为:{}'.format(driver.window_handles))
print('当前所在的窗口为: {}'.format(driver.current_window_handle))
sleep(3)
handles = driver.window_handles
print('所有的窗口句柄:{}'.format(handles))
for handle in handles:
if handle != hs:
driver.switch_to.window(handles[-1]) #hs
# for handle in hs:
# driver.switch_to.window(handle)
# print('切换到窗口handle:{}'.format(handle))
# print(driver.page_source)
# sleep(3)
# if driver.page_source.find('https://servicewechat.com/preload/page-frame.html') != -1:
# break
# sleep(3)
#元素已经被封装,这里直接调用那个模块就行
for i in range(10):
new = 1
ys = driver.find_element(*HomePage.hb).click()
sleep(4)
new += i
print('第:{}次拆红包'.format(new))
# print('当前窗口:{}'.format(driver.current_window_handle))
print('complete')
window_size = driver.get_window_size()
print(window_size)
x = window_size['width']
y = window_size['height']
def swipe_right(driver, t=400, n=1):
window_size = driver.get_window_size()
x1 = window_size['width'] * 0.25
y1 = window_size['height'] * 0.5
x2 = window_size['width'] * 0.75
for i in range(n):
driver.swipe(x1, y1, x2, y1, t)
swipe_right(driver)
print('swipe_right')
小程序拆红包的一个自动化。