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,320 points
3,044 views

1 answer to this question.

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,910 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer

How to extract text from a web page using selenium and save it as a text file?

Hello Isha, you can checkout this code ...READ MORE

answered May 7, 2019 in Selenium by Anvi
• 14,150 points
33,723 views
0 votes
1 answer

Using the same in IE browser but getting half the screen as black and half is coming

You could probably give a try making ...READ MORE

answered May 4, 2020 in Selenium by Sirajul
• 59,230 points
1,260 views
0 votes
2 answers

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,500 points
13,676 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,144 views
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,910 points
7,221 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,875 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,910 points
2,755 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,910 points
2,013 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP