How do I get the web page contents from a WebView?


On Android, I have a WebView that is displaying a page.

How do I get the page source without requesting the page again?

It seems WebView should have some kind of getPageSource() method that returns a string, but alas it does not.

If I enable JavaScript, what is the appropriate JavaScript to put in this call to get the contents?

webview.loadUrl("javascript:(function() { " +  
    "document.getElementsByTagName('body')[0].style.color  'red'; " +  


I know this is a late answer, but I found this question because I had the same problem. I think I found the answer in this post on The code below is basically a cut-and-paste from the site. It seems to do the trick.

final Context myApp  this;

/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface
    public void processHTML(String html)
        // process the html as needed by the app

final WebView browser  (WebView)findViewById(;
/* JavaScript must be enabled if you want it to work, obviously */

/* Register a new JavaScript interface called HTMLOUT */
browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");

/* WebViewClient must be set BEFORE calling loadUrl! */
browser.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url)
        /* This call inject JavaScript into the page which just finished loading. */

/* load a web page */

Answered By – jluckyiv

Leave a Comment