问题描述
我试着填写一张表格从Android客户端应用程序一个web视图中。我知道这应该如何工作,但总是的getElementById我返回null。我试图在不同的网站。
I try to fill out a form inside a webview from the android client app. I know how it should work, but the getElementById always returns null for me. I tried it on different websites.
下面是我的榜样www.google.com。
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);
而MyWebView类(仅供参考)。
And the MyWebView class (only for information).
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)
但元素MIB应该在网站上。使用桌面浏览器(Chrome浏览器与移动模拟)一切工作正常。我不知道,这里走错了什么。
But the element "mib" should be on the site. Using a desktop browser (chrome with mobile simulation) everything is working fine. I have no idea, whats going wrong here.
感谢您的提示!
编辑:我得到了一些进展。关于此网站,我还需要setDomStorageEnabled(真)。不,我可以找到DOM对象和设置的值,但不是表示改进后的网站我得到一个新的空白的,只有我设置的值。例如。一个白色的空白网站文本AAA。
I got some progress. Regarding 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 blank one with only the value I set. E.g. a white blank site with the text "aaa".
推荐答案
最后,我找到了解决办法!它是一个非常奇怪的现象。
Finally I found a solution! And it is a really strange behaviour.
首先你需要指定setDomStorageEnabled(真)对你的WebView。否则,DOM不起作用。我不知道为什么没有教程发出一个人。但确定。
First of all you need to specify setDomStorageEnabled(true) on your webview. Otherwise the DOM doesn't work. I wonder why no tutorial gave a hint about. But ok.
myview.getSettings().setDomStorageEnabled(true);
在此我结束了在一个白色的空白页,只有我设置的值。但奇怪的是,那的javascript:的document.getElementById('MyField的')值='AAA';
返回一个值。即一个我设置。因此,一个新的空白页面创建一个只包含字符串AAA。
After this I ended up in a white blank page with only the value I set. The strange thing is, that javascript:document.getElementById('myfield').value = 'aaa';
returns a value. Namely the one I set. So a new blank page was created that only contains the string "aaa".
我通过修改JavaScript来扔掉返回的结果解决了这个问题:
I solved it by modifying the javascript to throw away the return result:
javascript:var x = document.getElementById('myfield').value = 'aaa';
瞧。这是工作。
And voilá. It is working.
这篇关于机器人的WebView总是对JavaScript的getElementById上使用loadURL返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!