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) {
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".
java javascript android webview
Catscratch Sep 01 '14 at 8:52 2014-09-01 08:52
source share