I'm trying to interact with my web view so I can get a value to later insert into a file (which is not important), When trying to execute the JavaScript I tried adding script tags and also removing the function which both ways don't work. I don't get any error in any way at all. How do I execute JavaScript in a loaded web view?
WebView myWebView = (WebView) findViewById(R.id.webload);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://www.example.co.uk");
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
view.evaluateJavascript("(function(){alert('e');})();", givenValue -> {
if (givenValue != null && !givenValue.isEmpty() && !givenValue.equals("null")) {
}
});
super.onPageFinished(view, url);
}
}
);
Hey i have worked used webview alot in my recent projects and the right way to execute the javascript in a loaded webpage is below.
Add this line in onPageFinished function of WebViewClient. also don't forget to enable javascript of your webview