Appium 【求救】使用 Appium 框架测试 iOS 混合 APP,获取不到 WEBVIEW 页面

Tessa · 2021年10月10日 · 最后由 Eason 回复于 2021年10月12日 · 3317 次阅读

问题描述:
使用 Appium 框架测试 iOS APP ,该 APP 整个首页都是 H5,于是登陆进入 APP 后,我就开始打印 Contents,但是打印出来的只有 Native_APP,并没有 Web View 页面。

from appium import webdriver
import time
class Test01:
    server = 'http://localhost:4723/wd/hub'
    desired_caps = {'xcodeOrgId': "***********",
                    'xcodeSigningId': "iPhone Developer",
                    'udid': '**********',
                    'platformName': 'ios',
                    'platformVersion': '13.6.1',
                    'deviceName': 'iPhone7p',
                    'automationName': 'XCUITest',
                    'bundleId': '######',
                    "startIWDP": True
                    }
    driver = webdriver.Remote(server, desired_caps)

    def web_view(self):
        '''登陆按钮'''
    driver.find_element_by_xpath("//XCUIElementTypeButton[@name='登录']").click()
    '''账号输入框'''
    driver.find_element_by_xpath("//XCUIElementTypeApplication[@name='******']"
                                                 "/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/"
                                                 "XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/"
                                                 "XCUIElementTypeOther/XCUIElementTypeTextField").click() #点击

    driver.find_element_by_xpath("//XCUIElementTypeApplication[@name='*******']"
                                                 "/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/"
                                                 "XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/"
                                                 "XCUIElementTypeOther/XCUIElementTypeTextField").clear() #清空

    driver.find_element_by_xpath("//XCUIElementTypeApplication[@name='*********']"
                                                 "/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/"
                                                 "XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/"
                                                 "XCUIElementTypeOther/XCUIElementTypeTextField").send_keys("19520518894") #输入账号
    ''''密码输入框'''
    driver.find_element_by_xpath("//XCUIElementTypeApplication[@name='*******']"
                                             "/XCUIElementTypeWindow[1]/XCUIElementTypeOther/"
                                             "XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/"
                                             "XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeSecureTextField").click() #点击密码框

    driver.find_element_by_xpath("//XCUIElementTypeApplication[@name='*******']"
                                 "/XCUIElementTypeWindow[1]/XCUIElementTypeOther/"
                                 "XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/"
                                 "XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeSecureTextField").clear() #清空

    driver.find_element_by_xpath("//XCUIElementTypeApplication[@name='*******']"
                                 "/XCUIElementTypeWindow[1]/XCUIElementTypeOther/"
                                 "XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/"
                                 "XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeSecureTextField").send_keys("111111") #输入框

    driver.find_element_by_xpath("//XCUIElementTypeApplication[@name='*******']"
                                 "/XCUIElementTypeWindow[1]/XCUIElementTypeOther/"
                                 "XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/"
                                 "XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeSecureTextField").send_keys("\n")  # 输入框

    driver.implicitly_wait(60)  # 隐式等待15秒
    for i in range(10):
        print(driver.contexts)
        time.sleep(1)

控制台打开的结果:

Appium 的打印日志如下:

[HTTP] <-- GET /wd/hub/session/cb549267-532f-487e-bb1b-ff576d39f680/contexts 200 6 ms - 24
[HTTP] 
[HTTP] --> GET /wd/hub/session/cb549267-532f-487e-bb1b-ff576d39f680/contexts
[HTTP] {}
[W3C (cb549267)] Calling AppiumDriver.getContexts() with args: ["cb549267-532f-487e-bb1b-ff576d39f680"]
[XCUITest] Executing command 'getContexts'
[XCUITest] Getting list of available contexts
[iOS] Retrieving contexts and views
[XCUITest] Selecting by url: false 
[RemoteDebugger] Sending connection key request
[RemoteDebugger] Sending '_rpc_reportIdentifier:' message (id: 10): 'setConnectionKey'
[RemoteDebugger] Sending to Web Inspector took 2ms
[RemoteDebugger] Selecting application
[RemoteDebugger] No applications currently connected.
[XCUITest] No web frames found.
[W3C (cb549267)] Responding to client with driver.getContexts() result: ["NATIVE_APP"]

有没有大佬能帮我看看,谢谢了

共收到 3 条回复 时间 点赞

你使用 getPageResource 打印下页面信息,ios 有一些可以不用切换 webview 的,直接用 accessid 定位

Eason 回复

你好,我使用了 pycharm 提示:

/Users/mac/Desktop/Demo/Test/venv/bin/python /Users/mac/Desktop/Demo/Test/test_wingto/test_01.py
Traceback (most recent call last):
  File "/Users/mac/Desktop/Demo/Test/test_wingto/test_01.py", line 3, in <module>
    class Test01:
  File "/Users/mac/Desktop/Demo/Test/test_wingto/test_01.py", line 62, in Test01
    print(driver.getPageResource)
AttributeError: 'WebDriver' object has no attribute 'getPageResource'

使用 driver.page_source 打印的是原生页面的 page source

Tessa 回复

如果是自己开发的应用,需要开启 webview 的权限,可以联系下开发。如果是微信的话,安卓需要配一下 chromeOption,ios 以前我做好像是不用切换 webview 的,不太记得了

Tessa 关闭了讨论 11月18日 00:37
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册