Android WebView always returns null for javascript getElementById on loadUrl

I am trying to fill out a form inside a webview from an Android client application. I know how this should work, but getElementById always returns null for me. I tried this on different sites.

Here is my example for www.google.com.

MyWebView view = new MyWebView(this); view.getSettings().setJavaScriptEnabled(true); view.loadUrl("http://www.google.com"); view.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView v, String url) { v.loadUrl(url); return true; } @Override public void onPageFinished(WebView v, String url) { v.loadUrl("javascript:document.getElementById('mib').value = 'aaa';"); } }); setContentView(view); 

And the MyWebView class (for information only).

 class MyWebView extends WebView { Context context; public MyWebView(Context context) { super(context); this.context = context; } @Override public boolean onTouchEvent(MotionEvent event) { // do some multi touch stuff return true; } } 

I always get the error:

 09-01 04:35:26.453: I/chromium(2962): [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'value' of null", source: https://www.google.de/?gws_rd=ssl (1) 

But the element "mib" must be on the site. Using a desktop browser (chrome with a mobile simulator), everything works fine. I have no idea what is going on here.

Thanks for the tips!

Edit: I had some progress. As for this site, I also need setDomStorageEnabled (true). No. I can find the DOM object and set the value, but instead of showing the modified site, I get a new empty one, only with the value set. For example. white blank website with text "aaa".

+9
java javascript android webview
Sep 01 '14 at 8:52
source share
3 answers

Finally, I found a solution! And this is really strange behavior.

First of all you need to specify setDomStorageEnabled (true) in your webview. Otherwise, the DOM does not work. I wonder why not a single textbook suggested. But ok.

 myview.getSettings().setDomStorageEnabled(true); 

After that, I got to a white blank page with only the value that I set. It is strange that javascript:document.getElementById('myfield').value = 'aaa'; returns the value. Namely the one that I installed. Thus, a new blank page was created that contains only the string "aaa".

I solved this by modifying javascript to discard the return result:

 javascript:var x = document.getElementById('myfield').value = 'aaa'; 

And voila. He works.

+31
Sep 01 '14 at 12:39 on
source share

I also struggled with this problem for a long time. I had the same problem with a blank page as @Catscratch and nothing worked. Oddly enough, if you wrap the same code in a Javascript function, it all works.

In this way,

  webView.loadUrl("javascript:(function() { document.getElementById('email_field').value = '" + email + "'; })()"); 

works well

 webView.loadUrl("javascript:document.getElementById('email_field').value = '" + email + "';"); 

does not work.

And all this without , needing setDomStorageEnabled(true) and without setWebChromeClient()

+10
Nov 16 '15 at 19:49
source share

Another work around seems to work: make sure javascript "returns" null, ex:

 loadUrl("javascript:document.getElementById('myfield').value = 'aaa'; null"); 

Oddly enough, if you do not use getElementById , then OK for loadUrl returns values, the same behavior does not occur. It smells like some kind of error ... it may be this error , but may not be fixed in some versions of android [?]

0
Oct 10 '17 at 4:37 on
source share



All Articles