Android WebView 总是在 loadUrl 上为 javascript getElementById 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25602117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:43:05  来源:igfitidea点击:

Android WebView always returns null for javascript getElementById on loadUrl

javajavascriptandroidwebview

提问by Catscratch

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.

我尝试在 android 客户端应用程序的 webview 中填写表单。我知道它应该如何工作,但 getElementById 总是为我返回 null。我在不同的网站上尝试过。

Here is my example for www.google.com.

这是我的 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 (only for information).

和 MyWebView 类(仅供参考)。

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" 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.

但是元素“mib”应该在网站上。使用桌面浏览器(带有移动模拟的 chrome)一切正常。我不知道,这里出了什么问题。

Thanks for hints!

感谢您的提示!

Edit: I got some progress. Regarding thissite, 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".

编辑:我取得了一些进展。关于这个站点,我还需要 setDomStorageEnabled(true)。不,我可以找到 DOM 对象并设置值,但是我没有显示修改后的站点,而是得到一个新的空白站点,其中只有我设置的值。例如,带有文本“aaa”的白色空白站点。

回答by Catscratch

Finally I found a solution! And it is a really strange behaviour.

最后我找到了解决方案!这是一种非常奇怪的行为。

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.

首先,您需要在 webview 上指定 setDomStorageEnabled(true)。否则 DOM 不起作用。我想知道为什么没有教程给出提示。但是没问题。

myview.getSettings().setDomStorageEnabled(true);

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:document.getElementById('myfield').value = 'aaa';返回一个值。即我设置的那个。因此创建了一个仅包含字符串“aaa”的新空白页面。

I solved it by modifying the javascript to throw away the return result:

我通过修改javascript以丢弃返回结果来解决它:

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

And voilá. It is working.

瞧。这是工作。

回答by Stefan Anca

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

我也在这个问题上挣扎了很长一段时间。我在空白页上遇到了与@Catscratch 相同的问题,但没有任何效果。奇怪的是,如果您将相同的代码包装在 Javascript 函数中,则一切正常。

Thus,

因此,

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

works, while

工作,而

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

does not work.

不起作用。

And all of this withoutneeding setDomStorageEnabled(true)and without setWebChromeClient()

而这一切,而不需要setDomStorageEnabled(true)和不setWebChromeClient()

回答by rogerdpack

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

另一个似乎有效的解决方法:确保你的javascript“返回”null,例如:

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

Oddly however, if you aren't using getElementByIdthen it's OK for loadUrl to return values, the same behavior doesn't occur. Smells like some kind of bug...it might be this bugbut possibly not fixed in some versions of android [?]

然而奇怪的是,如果您不使用,getElementById则 loadUrl 返回值是可以的,不会发生相同的行为。闻起来像某种错误……可能是这个错误,但在某些版本的 android [?]