APP内嵌H5定位方法
方法:定位app内元素时用appium定位方法定位,定位H5页面元素时用selenium定位方法定位.他们之间的切换用contexts切换
第一步 得到APP和H5的contexts值
1
2 contexts = driver.contexts
print(contexts)
这里得到的第一个值为APP的contexts,第二个为selenium的contexts
so
1
2 context_appium = contexts[0]
context_selenium = context[1]
第二步 appium和selenium之间的切换
appium切换到selenium
driver.switch_to.contexts('WEBVIEW_com.wondershare.drfone')
或driver.switch_to.contexts(context_selenium)
selenium切换到appium
driver.switch_to.context('NATIVE_APP')
或driver.switch_to.contexts(context_appium)
实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time
desired_caps={}
desired_caps['platformName']='Android'
desired_caps['platformVersion']='5.1.1'
desired_caps['deviceName']='127.0.0.1:21503'
desired_caps['app']=r'H:\测试学习\dr.fone3.2.0.apk'
desired_caps['appPackage']='com.wondershare.drfone'
desired_caps['appActivity']='com.wondershare.drfone.ui.activity.WelcomeActivity'
driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
driver.implicitly_wait(8)
time.sleep(5)
driver.find_element_by_id('com.wondershare.drfone:id/btnBackup').click()
WebDriverWait(driver,10).until(lambda x:x.find_element_by_id('com.wondershare.drfone:id/btnRecoverData'))
driver.find_element_by_id('com.wondershare.drfone:id/btnRecoverData').click()
WebDriverWait(driver,10).until(lambda x:x.find_element_by_class_name('android.webkit.WebView'))
contexts=driver.contexts
print(contexts)
context_selenium = contexts[1]
context_appium = contexts[0]
#driver.switch_to.context(context_selenium)
driver.switch_to.context('WEBVIEW_com.wondershare.drfone')
driver.find_element_by_id('email').send_keys('shuqing2018@163.com')
driver.find_element_by_class_name('btn_send').click()
#driver.switch_to.context(context_appium)
driver.switch_to.context('NATIVE_APP')
driver.find_element_by_class_name('android.widget.ImageButton').click()