webview定位 & native和webview切换

前言:现在的app大都是混合式的native+webview,对于native可以直接用uiautomator定位然后操作元素,但是web就定位不到了

一、先看看使用native定位的

二、定位webview

1、对于可以抓接口的页面,使用fiddler抓取webview的请求链接,放到浏览器里面

2.对于不能抓接口的控件

  • android手机连接PC, 并确保在cmd中输入adb devices 可以找到devices的序列号.
  • 打开APP的webview 页面。

在 PC机的chrome 浏览器上输入如下地址:chrome://inspect/#devices

点击 上图中的inpect,你可以看到你手机的webview页面已经在PC上了,如下图。如果你点击了inpect跳出来的页面是空白的,原因你的浏览器没翻墙。 3、代码操作

 前面省略
************
click_findElement(self.driver, (By.ID, 'com.yuedan:id/iv_share'))  #分享按钮
print self.driver.contexts    
self.driver.implicitly_wait(10)
self.driver.execute(MobileCommand.SWITCH_TO_CONTEXT, {"name": "WEBVIEW_com.yuedan"}) #切换到webview上去
shareNum = self.driver.find_elements_by_css_selector('div.shareNew_center>ul>li')
print len(shareNum)
shareNum[0].click()
self.driver.execute(MobileCommand.SWITCH_TO_CONTEXT, {"name": "NATIVE_APP"})   #回到native上
self.driver.implicitly_wait(20)
tittext = get_findElement(self.driver, (By.ID, 'com.tencent.mm:id/jj'))
self.assertEqual(tittext, u'微信号/QQ/邮箱登录')
原文地址:https://www.cnblogs.com/guo2733/p/10559790.html