从原生页面切换到 webview 界面 driver.switch_to.context

本贴最后更新于 444 天前,其中的信息可能已经时移世异

'''

1、下载对应版本的谷歌驱动(下载地址:https://npmmirror.com/)

2、添加谷歌驱动"chromedriverExecutableDir":r"D:\chromedriver_win32"#添加谷歌驱动

3、切换到web页面:

#打印所有上下文

print(driver.contexts)

#切换到下个网页,H5网页

driver.switch_to.context(driver.contexts[-1])

4、打开谷歌浏览器,输入:chrome://inspect(谷歌浏览器开发者调试工具,打开会报404,需要翻墙才能使用,所以一般使用UC开发者工具调试)

或使用UC开发者工具定位web元素(需要设置》选择:本地DevtoolsInspectorUI资源否则打开页面会空白)

5、在谷歌浏览器识别到设备,及网页后,点击底部"inspect",进入开发者页面,进行元素定位

或用UC开发者工具进行元素定位(同样点击底部inspect)

'''

import time

from appium.webdriver import Remote

caps={

"platformName":"Android",

"udid":"127.0.0.1:5555",

"appPackage":"com.lemon.lemonban",#包名,手机安装的app的id

"appActivity":".activity.WelcomeActivity",#打开的APP页面名称

"chromedriverExecutableDir":r"D:\chromedriver_win32"#添加谷歌驱动

}

driver=Remote(

command_executor='http://127.0.0.1/wd/hub',

desired_capabilities=caps)

#隐性等待10s

driver.implicitly_wait(10)

#点击首页师资团队,进入H5页面

teacher_path="//*[@text='师资团队'and@resource-id='com.lemon.lemonban:id/category_title']"

el_teacher=driver.find_element("xpath",teacher_path)

el_teacher.click()

#打印所有上下文

print(driver.contexts)

#上下文切换

driver.switch_to.context(driver.contexts[-1])#切换到下个网页,H5网页

time.sleep(5)#强制等待5s

driver.quit()

回帖
请输入回帖内容 ...