Appium Inspector中将WebView视为原生应用,并且无法切换到WebView以查找元素。
创始人
2024-09-10 09:01:04
0

在 Appium Inspector 中将 WebView 视为原生应用,并且无法切换到 WebView 以查找元素的问题,可以通过以下方法解决:

解决方法1:使用 Chrome DevTools Protocol

  1. 启动 Appium 服务器时,添加以下参数:

    --relaxed-security
    --chromedriver-executable=/path/to/chromedriver
    --webview-connect-timeout=20000
    
  2. 在测试脚本中使用以下代码切换到 WebView:

    from appium import webdriver
    from appium.webdriver.common.mobileby import MobileBy
    
    desired_caps = {
        'platformName': 'Android',
        'deviceName': 'device',
        'appPackage': 'your_app_package',
        'appActivity': 'your_app_activity',
        'automationName': 'UiAutomator2',
        'chromedriverExecutable': '/path/to/chromedriver'
    }
    
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
    
    # 切换到 WebView
    contexts = driver.contexts
    for context in contexts:
        if 'WEBVIEW' in context:
            driver.switch_to.context(context)
            break
    
    # 使用 Chrome DevTools Protocol 查找元素
    element = driver.find_element(MobileBy.ACCESSIBILITY_ID, 'element_id')
    
    # 切换回原生应用
    driver.switch_to.context('NATIVE_APP')
    

解决方法2:使用 WebdriverIO

  1. 安装 WebdriverIO:

    npm install webdriverio
    
  2. 在测试脚本中使用以下代码切换到 WebView:

    const webdriverio = require('webdriverio');
    
    const options = {
        path: '/wd/hub',
        port: 4723,
        capabilities: {
            platformName: 'Android',
            deviceName: 'device',
            appPackage: 'your_app_package',
            appActivity: 'your_app_activity',
            automationName: 'UiAutomator2',
            chromedriverExecutable: '/path/to/chromedriver'
        }
    };
    
    async function main() {
        const client = await webdriverio.remote(options);
    
        // 切换到 WebView
        const contexts = await client.getContexts();
        for (const context of contexts) {
            if (context.includes('WEBVIEW')) {
                await client.switchContext(context);
                break;
            }
        }
    
        // 使用 WebdriverIO 查找元素
        const element = await client.$('~element_id');
    
        // 切换回原生应用
        await client.switchContext('NATIVE_APP');
    
        await client.deleteSession();
    }
    
    main();
    

通过以上方法,你可以在 Appium Inspector 中将 WebView 视为原生应用,并且通过代码切换到 WebView 来查找元素。请注意,你需要将 /path/to/chromedriver 替换为你本地的 chromedriver 路径,并将 your_app_packageyour_app_activity 替换为你的应用的包名和活动名。

相关内容

热门资讯

经调查!aapoker ai插... 经调查!aapoker ai插件,德普之星app安卓版破解版,果然确实有挂(有挂讲解)1、游戏颠覆性...
玩家必看科普!福建天天开心辅助... 玩家必看科普!福建天天开心辅助软件,超级三加一视频,详细方法(了解有挂)1、福建天天开心辅助软件模拟...
有了最新消息!hhpoker有... 有了最新消息!hhpoker有没有作弊辅助,德普之星辅助器怎么用,一贯存在有挂(有挂方略)1、德普之...
盘点一款!闲逸游戏辅助软件下载... 盘点一款!闲逸游戏辅助软件下载,博雅棋牌辅助器,详细方法(有挂秘诀)1、下载好闲逸游戏辅助软件下载正...
一直以来!agpoker辅助,... 一直以来!agpoker辅助,德普之星辅助工具如何设置,一直是有挂(有挂神器)1.德普之星辅助工具如...
玩家科普!wepoker辅助,... 玩家科普!wepoker辅助,宝宝游戏辅助,详细脚本(有挂功能)宝宝游戏辅助透视方法中分为三种模型:...
2026版教程!wpk作弊是真... 2026版教程!wpk作弊是真的吗,智星德州辅助译码插件靠谱吗,原来真的是有挂(有挂详情)智星德州辅...
必看攻略!川南休闲辅助,微信老... 必看攻略!川南休闲辅助,微信老友广东辅助,详细器(详细教程)微信老友广东辅助破解侠是真的助透视。每个...
今天下午!wepoker免费透... 今天下午!wepoker免费透视脚本,德普之星透视辅助软件是真的吗,真是是真的有挂(今日头条)德普之...
实测教程!微信小程序外辅助工具... 实测教程!微信小程序外辅助工具,网易亲友圈辅助,详细工具(有挂功能)1、这是跨平台的微信小程序外辅助...