少年辛苦终身事,莫向光阴惰寸功。这篇文章主要讲述Appium 解决微信公众号小程序切换 webview 后无法定位元素的问题相关的知识,希望能为你提供帮助。
【Appium 解决微信公众号小程序切换 webview 后无法定位元素的问题】如何切换webview进入小程序请参考https://testerhome.com/topics/12003
脚本思路:
进入webview后会存在多个handle同Web页签一样,获取所有的handle,然后在遍历所有的handle,通过switch_to_window进行handle切换,当某个handle可以定位到我们需要定位的元素时,然后我们就可以进行之后的自动化操作了!
- < code style="font-family:Menlo, Monaco, Consolas, ‘Courier New‘, monospace; line-height:18px; font-size:12px; color:rgb(68,68,68); border:none; "> #coding=utf-8
- from appium import webdriver
- import time,os,re
- from appium.webdriver.common.touch_action import TouchAction
- desired_caps = {}
- desired_caps[‘platformName‘] = ‘android‘
- desired_caps[‘deviceName‘] = ‘Android001‘
- desired_caps[‘unicodeKeyboard‘] = True
- desired_caps["resetKeyboard"] = True
- desired_caps["newCommandTimeout"]=30
- desired_caps[‘fullReset‘] = ‘false‘
- desired_caps[‘appPackage‘] = ‘com.tencent.mm‘
- desired_caps[‘appActivity‘] = ‘com.tencent.mm.ui.LauncherUI‘
- desired_caps[‘recreateChromeDriverSessions‘] = True
- desired_caps[‘noReset‘] = True
- desired_caps[‘newCommandTimeout‘] = 10
- desired_caps[‘chromeOptions‘]={‘androidProcess‘: ‘com.tencent.mm:appbrand0‘}
- driver = webdriver.Remote(command_executor = ‘http://127.0.0.1:4723/wd/hub‘,desired_capabilities = desired_caps)
- time.sleep(2)
- time.sleep(1)
- driver.implicitly_wait(10)
- driver.find_element_by_name(‘发现‘).click()
- time.sleep(1)
- driver.swipe(100,1200,100,900)
- driver.find_element_by_name(‘小程序‘).click()
- driver.find_element_by_name(‘美团外卖‘).click()
- time.sleep(2)
- contexts = driver.contexts
- print contexts
- time.sleep(2)
- driver.switch_to.context(‘WEBVIEW_com.tencent.mm:appbrand0‘)
- print ‘切换成功‘
- print driver.current_context
- all_handles = driver.window_handles
- print len(all_handles)
- for handle in all_handles:
- try:
- driver.switch_to_window(handle)
- print driver.page_source
- driver.find_element_by_css_selector(‘.filter-select.flex-center‘)#定位“筛选 ”按钮
- print ‘定位成功‘
- break
- except Exception as e:
- print e
- driver.find_element_by_css_selector(‘.filter-select.flex-center‘).click()
- time.sleep(5)
- driver.quit()< /code>
推荐阅读
- android appium微信等自动化的那些坑儿
- java/android 做题中整理的碎片小贴士(16)
- Delphi XE开发 Android 开机自动启动
- Swift运算符介绍和用法示例
- Swift字面量介绍和用法详解
- Swift常量介绍和用法示例
- Swift变量介绍和用法详解
- Swift基本语法介绍和用法详细介绍
- Swift中的数据类型解释和用法详解