appium操作H5应用

1.首先需要将包调成debug模式(将webview设置为debug模式,让开发开启),才可以操作H5页面

  解决方案:开发提供debug包

2.WebView里面就是H5页面

WebView是chrome浏览器的内核,安卓使用是的是谷歌浏览器的内核(安卓就是谷歌的),因此使用对应版本的chromedriver。

因此要使用手机webview对应的版本的chromedriver

查看手机webview版本:

1:手机设置中搜索webview查看对应版本信息

 

 

 

 2.如果要切换到H5(webview)的情况下,必须含有含有 对应版本webview的chromedrive

如果不知道自己手机的webview版本方式二:

1:先切到webview中,然后配置项中不传chromedriver地址。这样代码就会报错,我们可以从报错中知道webview的版本

 

 

 

切换webview代码实现:

from appium import webdriver

#混合app自动化
boss_desired_capabilities = {
    'platformName': 'Android',
    'plathformVersion': '10',
    'deviceName': 'boss_desired_capabilities',
    'appActivity': '.MainActivity',
    'appPackage': 'com.example.haiwen.myhybirdapp',
    'noReset': True,
    'newCommandTimeout': 6000,
    'automationName': 'UiAutomator2',
    #跳过UI2的安装,如果第一次运行程序,不要添加该配置
    #'skipServerInstallation':True
    #需要制定对应WebView版本的chrmoedriver 驱动
    'chromedriverExecutable':r'C:\Users\xibo.zhu\work\myself\myProject\myProject\appium_basicCode\context_chromedriver_win32\chromedriver.exe',  # 这里填写的是驱动所在的目录

}
#-----------------------------------控制原生控件-----------------------
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',boss_desired_capabilities)
#输入豆瓣地址
driver.find_element_by_id('com.example.haiwen.myhybirdapp:id/editText').send_keys('https://m.douban.com/home_guide')
#点击跳转按钮
driver.find_element_by_id('com.example.haiwen.myhybirdapp:id/button').click()

#----------------------------控制WebView(H5)------------------------------
#查看当前所在的context
print(driver.current_context) #原生的:NATIVE_APP
#获取所有的context
print(driver.contexts) #['NATIVE_APP', 'WEBVIEW_chrome', 'WEBVIEW_com.example.haiwen.myhybirdapp', 'WEBVIEW_com.huawei.browser']
#如果以html控件形式自动化webview需要切到webview对应的context
#切换webview成功的前提
#1.被测app开启webview debug模式----从app市场安装的不具备此条件
#2.需要选择符合该操作系统当前webiew版本的chromedriver

#切换到H5的WebView上
driver.switch_to.context('WEBVIEW_com.example.haiwen.myhybirdapp')
print(driver.current_context)#WEBVIEW_com.example.haiwen.myhybirdapp已经切换到webview了
#定位H5到输入框,搜索电影名称
driver.find_element_by_css_selector('[class="search-input"]').send_keys('肖生克救赎\n')
#获取电影评分
rate = driver.find_element_by_css_selector('body > div.page > div > div > ul > li:nth-child(2) > ul > li:nth-child(1) > a > div > p > span:nth-child(2)').text

print(f'电影评分是{rate}')
#如果要切回原生应用
# driver.switch_to.context('NATIVE_APP')
driver.quit()

 

posted @ 2021-01-12 20:53  西西cc  阅读(543)  评论(0编辑  收藏  举报