How to Retrieve HTML Content from Webview (As a String)

Get html content from webView in Swift?

You can get the inner text of the div by:

func webViewDidFinishLoad(_ webView: UIWebView) {

guard let text = webView.stringByEvaluatingJavaScript(from: "document.getElementById(\"displayMsg\").innerText") else {
return
}

print(text) // Thanh toán thành công
}

Get the HTML code from loaded WebView

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>');");
}
});

how to get html content form flutterWebViewPlugin ( webview ) in flutter

Well the answer for me was to get the token from a url and not from html content as I am not able to extract it properly and get errors

[VERBOSE-2:ui_dart_state.cc(166)] Unhandled Exception: FormatException: Unexpected character (at character 1)
<html><head></head><body><pre style="word-wrap: break-word; white-space: pr...
^

so what worked for me for the start is the below solution:

WebView(
userAgent: "random",
initialUrl: mainUrl,
onWebViewCreated: (controller) {
_controller = controller;
print(_controller.currentUrl());
},
javascriptMode: JavascriptMode.unrestricted,
gestureNavigationEnabled: true,
onPageFinished: (_) {
_controller.currentUrl().then(
(url) {
if (url.contains("recon?")) {
var token = url.split('recon?')[1];
_prefs.setString('token', token);
}
if (url.contains("dashboard")) {
_controller.clearCache();
_controller.loadUrl(mainUrl);
}
},
);
},
),

here we are taking the url, spliting it based on keyword and then storing the other half which is the token and continuing. I was able to do this in both ways that is with flutter_webview_plugin as well as flutter_inappwebview its the same procedure but URL thing only worked for me. extracting context didn't so they are not marked as an answer but an up vote for helping me out understanding the possibility.

How to access HTML source from Android WebView?

I found my quesntion's solution in this post.

https://stackoverflow.com/questions/6503574/how-to-get-html-source-code-from-url-in-android[][1]

I don't know much about Ion dependency but it did my work.

How to get the html content from UIWebView?

I think you have to evaluate the javascript like this:

let doc = webView.stringByEvaluatingJavaScriptFromString("document.documentElement.outerHTML")

In this case you get the entire HTML.



Related Topics



Leave a reply



Submit