1. 认识 webView
实例说明:
当你打开百度阅读 App→VIP 全站去广告→用自带的 UI Automator 去定位里面的元素, 如图:
不管你去定位 '规则详情' '开通'等等, 都会定位不到, 只能显示一个整体页面, 这个就是 WebView
Note:
1 可以理解与 selenium 里的 iframe 类似
2在右边定位里有明确的表示是 WebView
3点击一个链接后, 有进度条加载后页面一般都是 Webview. 也就是说, 是一个 H5 页面了
4UI Automator 不能定位到里面的元素
处理方法 1:
1执行 print(driver.contexts) 获取所有的上下文
2在切换到他的 webview 里面去 (类似 selenium 里的切换窗口)
3获取到他的 webview 源码, 在其他浏览器打开, 获取他的 xpath 路径, 或者其他定位方法
- #coding:utf-8
- from appium import webdriver
- import time
- u'''智行火车票, webview 页面定位'''
- desired_caps = {'platformName': 'Android',
- 'deviceName': '9a762346',
- 'platformVersion': '6.0.1',
- 'noReset': True,
- 'appPackage': 'com.yipiao',
- 'appActivity': 'com.zt.main.entrance.ZTLaunchActivity'}
- driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
- time.sleep(10)
- print(driver.context)
- driver.find_element_by_xpath('//*[@text=" 我的 "]').click()
- time.sleep(3)
- driver.find_element_by_xpath('//*[@text=" 产品意见 "]').click()
- time.sleep(3)
- print(driver.contexts)
- driver._switch_to.context('WEBVIEW_com.yipiao')
- print('切换成功')
- p=driver.page_source
- with open('1111.html','wb') as f:
- f.write(p.encode('utf-8'))
- time.sleep(2)
- #driver.find_element_by_xpath("//*[contains(text(),'[抢票] 我能抢到票吗')]").click()
- driver.find_element_by_xpath(".//*[@id='container']/div/div/section[2]/ul/li[2]/a/div[1]/span").click()
- 处理方法 2:
- 主要是你已经确定他是一个 webview, 但是打印全部上下文返回的 list 里只有 NATIVE_APP, 无法进行切换.
- 就不要去切换了, 当做是 APP 原生的, 只需要定位到某个元素就行
- 这种情况不建议用获取源码, 没太大用.
- #coding:utf-8
- from appium import webdriver
- import time
- u'''百度阅读, webview 页面定位'''
- desired_caps = {"platformName": "Android",
- "deviceName": "9a762346",
- "platformVersion": "6.0.1",
- "noReset": True,
- "appPackage": "com.baidu.yuedu",
- "appActivity": "com.baidu.yuedu.splash.SplashActivity"}
- driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
- time.sleep(10)
- driver.find_element_by_accessibility_id('VIP').click()
- time.sleep(3)
- driver.find_element_by_accessibility_id("规则详情").click()
来源: http://www.bubuko.com/infodetail-2851179.html