如何从hybird应用程序中获取AppiumDriver中的web元素?

时间:2016-03-07 17:08:51

标签: cordova selenium-webdriver automation appium hybrid-mobile-app

我正在尝试使用appium和selenium自动化Cordova构建hybird应用程序。

但是,我无法使用

获取任何元素
driver.findElement(By.id("xxx"));

当我使用

进行pageSource转储时
String pageSource = driver.getPageSource();

    System.out.print("*************dumping: ***********\n"+pageSource+"\n\n\n");

我正在

<?xml version="1.0" encoding="UTF-8"?><hierarchy rotation="0"><android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" package="com.alarmforce.connect.client.keypad" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1024,552]" resource-id="" instance="0"><android.widget.LinearLayout index="0" text="" class="android.widget.LinearLayout" package="com.alarmforce.connect.client.keypad" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1024,552]" resource-id="" instance="0"><android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" package="com.alarmforce.connect.client.keypad" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,25][1024,552]" resource-id="android:id/content" instance="1"><android.webkit.WebView index="0" text="" class="android.webkit.WebView" package="com.alarmforce.connect.client.keypad" content-desc="Web View" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="true" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,25][1024,552]" resource-id="" instance="0"/></android.widget.FrameLayout></android.widget.LinearLayout></android.widget.FrameLayout></hierarchy>

如何让它以HTML格式显示,就像我在连接设备时检查chrome:// inspect?

我试过了:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        WebView.setWebContentsDebuggingEnabled(true);
    }
MainActivity.java中的

我也尝试过:

driver.context("web_view");

能力:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName","xxxxxxx");
capabilities.setCapability("platformVersion", "4.4");
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

2 个答案:

答案 0 :(得分:0)

使用appium切换上下文的好方法是:

Set<String> contextNames = driver.getContextHandles();
String setContext = contextNames.toArray()[1].toString(); //generally your WEBVIEW context if available
driver.context(setContext);// set context to the webview name in string format

答案 1 :(得分:0)

感谢nullpointer,问题解决了!对于遇到此问题的任何人,请尝试以下方法:

    System.out.print("*************dumping source before setContext(unusable stuff): *************\n"+driver.getPageSource()+"\n\n\n"); 


    Set<String> contextNames = driver.getContextHandles();
    String setContext = contextNames.toArray()[1].toString(); //generally your WEBVIEW context if available
    driver.context(setContext);// set context to the webview name in string format

    System.out.print("*************dumping source after setContext(useful stuff): *************\n"+driver.getPageSource()+"\n\n\n");