Get the HTML code from loaded WebView [duplicate]

I have something like this

final WebView w=(WebView)findViewById(R.id.webView1);
w.loadUrl("http://somepage.com");

Is there any way to get the html page that is shown in the WebView at some moment of time ? I want to get this html page as string variable.

The point is I want to get the html code after the javascript is executed on a client side...

any guidelines ?

Answers:

Answer

One way I know;

decleration javascript handler in your activity

class LoadListener{
    public void processHTML(String html)
    {
        Log.e("result",html);
    }
}

after configure your webview;

webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new LoadListener(), "HTMLOUT");

than webview client;

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

                return true;
        }              

        @Override
        public void onPageStarted(WebView view, String url,
                        Bitmap favicon) {
        }

        public void onPageFinished(WebView view, String url) {
            view.loadUrl("javascript:window.HTMLOUT.processHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
        }
});
Answer

One way to get the code is by using the HttpClient as given here. Another solution is given in the following blog.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.