Fill out a form in WebView with Javascript

I am trying to populate Webforms from Webview in Android. I already found this code snippet here: Fill in the fields in the webview automatically

String username = "cristian"; webview.loadUrl("javascript:document.getElementById('username').value = '"+username+"';"); 

Unfortunately, I do not understand where I need to open the page that I want to fill out.

 setContentView(R.layout.web); final WebView mWebView = (WebView) findViewById(R.id.webview); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.loadUrl(url); String user="u"; String pwd="p"; mWebView.loadUrl("javascript:document.getElementById('username').value = '"+user+"';document.getElementById('password').value='"+pwd+"';"); 

When I try to do this, the site is displayed, but without any form values.

Thanks in advance for your help.

+12
javascript android android-webview
Apr 17 2018-12-12T00:
source share
4 answers

You must fill in the values ​​after the page loads. This is an example of using your code:

 mWebView.loadUrl(url); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { String user="u"; String pwd="p"; view.loadUrl("javascript:document.getElementById('username').value = '"+user+"';document.getElementById('password').value='"+pwd+"';"); } }); 
+19
Nov 12 '12 at 15:40
source share

On Android 4.4, the new WebView has a problem using the loadUrl("javascript:") method, the parameter string will be url-decode before execution. You can try using evaluateJavascript() for API> = 19 and loadUrl() for API <19. The code below works for me.

  mWebView.loadUrl(url) WebSettings settings = mWebView.getSettings(); settings.setJavaScriptEnabled(true); String js = "javascript:document.getElementById('username').value = '"+user+"';document.getElementById('password').value='"+pwd+"';"; mWebView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); if (Build.VERSION.SDK_INT >= 19) { view.evaluateJavascript(js, new ValueCallback<String>() { @Override public void onReceiveValue(String s) { } }); } else { view.loadUrl(js); } }); } 

see below: loadUrl behavior ("javascript: ....") changed incompatible in Android 4.4

+16
Aug 20 '15 at 10:11
source share

I had the same problem, and after the following various explanations on stackOverflow, I managed to get my work done.

here is my approach

 webView.loadUrl(url); webView.getSettings().setDomStorageEnabled(true); webView.setWebViewClient(new WebViewClient(){ public void onPageFinished(WebView view, String url) { String email="email@email.jp"; //view.loadUrl("javascript:document.getElementById('email').value = '"+email+"'"); view.loadUrl("javascript:document.forms[0].email.value = '"+email+"';"); Log.d("email", "can not add email"); } }); 

2 things: 1) you need to add this line webView.getSettings().setDomStorageEnabled(true); (reference: Android WebView always returns null for javascript getElementById on loadUrl )

2) you need to access the variable in your php code using this view.loadUrl("javascript:document.forms[0].email.value = '"+email+"';"); as you can see in my code, I used the solution suggested by @gnpaolo, but it didn’t work for me, so I commented on it and took advantage of it. (link: How to insert a row in Android WebView )

Finally, I just want to add that you do not need to create special javascript.

one more thing forms[0] is the position of the variable in the php form, and in my case I have the user's email address, so I wrote view.loadUrl("javascript:document.forms[0].email.value = '"+email+"';");

Hope this can help others.

0
Jul 03 '15 at 9:05
source share

Just turn on DomStorage and write "var x =" in the line:

 webview.getSettings().setJavaScriptEnabled(true); web.getSettings().setDomStorageEnabled(true); webview.loadUrl(urlString); webview.setWebViewClient(new WebViewClient(){ public void onPageFinished(WebView view, String url){ super.onPageFinished(view, url); String js = "javascript:var x=document.getElementById('username').value = '"+user+"';var y=document.getElementById('password').value='"+pass+"';"; if (Build.VERSION.SDK_INT >= 19) { view.evaluateJavascript(js, new ValueCallback<String>() { @Override public void onReceiveValue(String s) { } }); } else { view.loadUrl(js); } view.loadUrl(js); } }); 
0
Jan 19 '18 at 21:22
source share



All Articles