1. 程式人生 > 實用技巧 >appium 處理webview

appium 處理webview

開啟webview頁面

chrome://inspect/#devices

獲取webview driver版本

1、

2、

3、

chenshifengdeMacBook-Pro:ChromeDriver chenshifeng$ adb shell pm list package|grep webview
package:com.android.webview
package:org.chromium.webview_shell
chenshifengdeMacBook-Pro:ChromeDriver chenshifeng$ adb shell pm dump com.android.webview|grep
version versionCode=373018615 minSdk=21 targetSdk=29 versionName=74.0.3729.186

切換native與webview

#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:chenshifeng
@file:test_webview.py
@time:2020/11/29
"""

from appium import webdriver


class TestWebview:
    def setup(self):
        desired_caps 
= { "platformName": "Android", "platformVersion": "8.0", "automationName": "UiAutomator2", "deviceName": "emulator-5554", "appPackage": "com.example.android.apis", "appActivity": "com.example.android.apis.ApiDemos", "
chromedriverExecutable": "/Users/chenshifeng/Library/SoftWare/ChromeDriver/chromedriver74", # "appActivity": ".common.MainActivity", "noReset": True, # 不重置APP # "autoGrantPermissions": True, # "skipServerInstallation": True, # 跳過 uiAutomator2伺服器安裝 # "dontStopAppOnReset": True, # "skipDeviceInitialization": True, # 跳過裝置初始化 # "unicodeKeyboard": True, # 預設啟用 Unicode 輸入 # "resetKeyboard": True, # 與上面合用,可以輸入中文 # "newCommandTimeout": 300 } self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) self.driver.implicitly_wait(10) def teardown(self): self.driver.quit() def test_webview(self): self.driver.find_element_by_xpath('//*[contains(@text,"Views")]').click() print(self.driver.contexts) self.driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().' 'scrollable(true).instance(0)).' 'scrollIntoView(new UiSelector().text("WebView").' 'instance(0));').click() # self.driver.find_element_by_xpath('//*[contains(@text,"Hello World! - 1")]').click() # 可以與native頁面一樣處理,但這種方法由於不同的手機渲染的結果不一樣,不具有通用性 print(self.driver.contexts) # 獲取上下文 self.driver.switch_to.context(self.driver.contexts[-1]) 進入到webview頁面 self.driver.find_element_by_xpath('/html/body/a').click() # print(self.driver.page_source) sleep(5)

執行結果

============================== 1 passed in 32.47s ==============================

Process finished with exit code 0
PASSED                        [100%]['NATIVE_APP']
['NATIVE_APP', 'WEBVIEW_com.example.android.apis']