Getting android.webkit.WebView contents as a String using Appium

0 votes

How to get the contents of a WebView as a String using Appium. I am using the Android driver.

Below is the code I tried

androidDriver.getPageSource();

It throws me the below XML.

<?xml version="1.0" encoding="UTF-8"?><hierarchy rotation="0"><android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" package="com.test.program" 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][768,1184]" resource-id="" instance="0"><android.widget.LinearLayout index="0" text="" class="android.widget.LinearLayout" package="com.test.program" 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][768,1184]" resource-id="" instance="0"><android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" .......... resource-id="android:id/navigationBarBackground" instance="201"/></android.widget.FrameLayout></hierarchy>

Aug 6, 2018 in Selenium by Martin
• 4,210 points
310 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Below will help you:

public final String WEBVIEW = "WEBVIEW_com.maxsoft.poker";

public final String NATIVE_APP = "NATIVE_APP";

public String getHTMLPageSource(String url){

    switchContextTo(WEBVIEW);

    getDriver().navigate().to(url);

    return getDriver().getPageSource();

}

public void switchContextTo(String context){

    if (context.toLowerCase().equals(WEBVIEW.toLowerCase())) {

        getDriver().context(WEBVIEW); // set context to WEBVIEW_1

    } else {

        getDriver().context(NATIVE_APP); // set context to NATIVE_APP

    }

}

You didn't switch the context from native app to webview. So that getPageSource()will return the xml content which is relevant to the current activity. So from switchContextTo(String context) method I switch the current context from native app to webview and from getHTMLPageSource(String url) I am getting the webview content as a String

answered Aug 6, 2018 by Samarpit
• 5,130 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

I tried to open a new website when a new tab is opened using Selenium Webdriver

Try this code: ArrayList<String> tabs = new ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,030 points
482 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,030 points
73 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
446 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
55 views
0 votes
1 answer

How to login a forum using Selenium with Python

You should try to directly log in ...READ MORE

answered Apr 27, 2018 in Selenium by Samarpit
• 5,130 points
157 views
0 votes
1 answer

Opening Tabs using a Watir in Ruby

Selenium doesn't explicitly support opening of tabs, ...READ MORE

answered May 10, 2018 in Selenium by Samarpit
• 5,130 points
341 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.