A
A
Andrey2017-03-07 11:39:04
JavaScript
Andrey, 2017-03-07 11:39:04

Why is javascipt in webview not fully working?

There is a page that is loaded using the okHttp library,
after which html is parsed and the necessary data is retrieved.
The next step is to load the page in the webview.

webView.loadDataWithBaseURL(null, result, "text/html", "UTF-8", null);

After some manipulations with the data, I try to inject them back onto the page and they are still injected, only there is a problem that the send button is not pressed. Neither with a script nor in the webview itself.
Here is the javascript code responsible for inserting data and clicking the button
@Override
        public void onPageFinished(WebView view, String url) {
            view.loadUrl(
                    "javascript:(" +
                            "window.onload = function ready() { " +
                            "var btnOpen = document.getElementsByClassName('btn btn-primary js-ask');" +
                            "btnOpen[0].click();" +
                            "var btnSend = document.getElementsByClassName('btn btn-primary pull-right');" +
                            "var question = document.getElementById('id_question').value=" + question + ";" +
                            "var name = document.getElementById('id_user_name').value='" + name + "';" +
                            "var email = document.getElementById('id_email').value='" + email + "';" +
                            "var captchaEdit = document.getElementById('id_captcha_1').value='" + captcha + "';" +
                            "console.log('btnSend[0]);" +
                            "btnSend[0].click();" +
                            "})()");
        }

Console output from the onPageFinished method
"about:blank#", source: about:blank (1)
"[object HTMLInputElement]", source: about:blank (1)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey, 2017-03-14
@poslannikD

After loading data from the web (webView.loadUrl(" http://... ");) and injecting everything worked fine. For those who come across this situation: load the page immediately from the Internet without dancing with a tambourine.

D
Denis Zagaevsky, 2017-03-07
@zagayevskiy

Firstly, it seems that your code is not a url after all, and stuffing it into loadUrl is at least strange. Xs, not a webview guru %)
Secondly, you need to try to enable JavaScript, in my opinion, it is disabled by default:webView.getSettings().setJavaScriptEnabled(true);

M
Makhach Imangazaliev, 2017-03-07
@ImangazalievM

Try the Scripto library . She will solve your problems

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question