Removing div tag from url loaded from webview android

android, android-webview, webview

Solution

You can use getElementsByClassName() to access the div. Here is my solution please try it.

web.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url){

            web.loadUrl("javascript:document.getElementsByClassName('header')[0].style.display=\"none\";");

        }
    })

Problem

I am loading a url into webview using web.loadUrl(url);. Now i want to remove part the body content when the url finished loading. Data from the url is as follows : ``` <html> <body> <div class="header" data-role="header" data-theme="a"> <a data-icon="back" class="header-icon" data-iconpos="notext" href="mymob-web-mobile/restricted/menu.xhtml" data-ajax="false"> <span>Back</span> </a> <!--Title--> <h1>???help.main.title???</h1> </div> <div id="well">Hello World</div> <body> </html> ``` I want to remove this part in the url ``` <div class="header" data-role="header" data-theme="a"> <a data-icon="back" class="header-icon" data-iconpos="notext" href="mymob-web-mobile/restricted/menu.xhtml" data-ajax="false"> <span>Back</span> </a> <!--Title--> <h1>???help.main.title???</h1> </div> ``` After some researh i come to this solution: ``` web= (WebView)findViewById(R.id.web); web.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { web.loadUrl("javascript:var con = document.getElementByTagName('<div class=\"header\" data-role=\"header\" data-theme=\"a\"> '); " + "con.style.display = 'none'; "); } }); web.clearCache(true); web.clearHistory(); web.getSettings().setJavaScriptEnabled(true); web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); web.loadUrl(Constant.URL_AIDE, headers); ``` But the div element is not being removed. a summery is , div to remove ``` <div class="header" data-role="header" data-theme="a"> <a data-icon="back" class="header-icon" data-iconpos="notext" href="mymob-web-mobile/restricted/menu.xhtml" data-ajax="false"> <span>Back</span> </a> <!--Title--> <h1>???help.main.title???</h1> </div> ``` expected result ``` <html> <body> <div id="well">Hello World</div> <body> </html> ``` any idea please

Original source