javascript - WebView,JavaScript 单击按钮的问题

标签 javascript java android jquery webview

我正在尝试单击网站上的按钮,我正在使用 JavaScript 加载 WebView,但到目前为止我还没有任何运气。

我尝试了两种新方法来按下按钮,但都没有成功:

第一种方法:

mWebView.loadUrl("javascript:var y = document.getElementById('form-login-submit'); y.click();");

第二种方法:

mWebView.loadUrl("javascript:(function(){document.getElementById('form-login-submit').click();})()");

错误:

02-18 11:18:40.985 1478-1478/com.example.okke.testwebapp03 I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'value' of null", source:  (1)
02-18 11:18:40.989 1478-1478/com.example.okke.testwebapp03 I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'value' of null", source:  (1)
02-18 11:18:40.989 1478-1478/com.example.okke.testwebapp03 I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot read property 'click' of null", source:  (1)

这是目前我执行所有 JS 函数的代码:

Log.d("IsJavaScriptEnabled?(2)", String.valueOf(mWebView.getSettings().getJavaScriptEnabled()));
Log.d("IsDomStorageEnabled?(2)", String.valueOf(mWebView.getSettings().getDomStorageEnabled()));
mWebView.loadUrl("javascript:var x = document.getElementById('modlgn-username').value = '" + userName + "';");
mWebView.loadUrl("javascript:var z = document.getElementById('modlgn-passwd').value = '" + passWord + "';");
mWebView.loadUrl("javascript:(function(){"+
          "var l=document.querySelector('#login-form [type="+'"'+"submit"+'"'+"]');"+
          "var e=document.createEvent('HTMLEvents');"+
          "e.initEvent('click',true,true);"+
          "l.dispatchEvent(e);"+
          "})()");

这些是我现在遇到的错误:

02-17 15:12:20.578 19646-19646/com.example.okke.testwebapp03 I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'value' of null", source:  (1)
02-17 15:12:20.580 19646-19646/com.example.okke.testwebapp03 I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot set property 'value' of null", source:  (1)
02-17 15:12:20.582 19646-19646/com.example.okke.testwebapp03 I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot read property 'dispatchEvent' of null", source:  (1)
02-17 15:12:22.147 19646-19646/com.example.okke.testwebapp03 W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 19646

我尝试过多种方法:

mWebView.loadUrl("javascript: var y = document.getElementsByName('Submit')[0]; y.click();");

这给了我这个错误:

02-17 11:25:20.008 5132-5132/com.example.okke.testwebapp03 I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot read property 'click' of undefined", source:  (1)
02-17 11:25:20.202 5132-5132/com.example.okke.testwebapp03 W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 5132
02-17 11:25:21.130 5132-5132/com.example.okke.testwebapp03 W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 5132
02-17 11:25:21.160 5132-5132/com.example.okke.testwebapp03 W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 5132
02-17 11:25:21.195 5132-5132/com.example.okke.testwebapp03 W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 5132

第二种方法:

mWebView.loadUrl("javascript:(function(){"+
                    "l=document.getElementsByName('Submit')[0];"+
                    "e=document.createEvent('HTMLEvents');"+
                    "e.initEvent('click',true,true);"+
                    "l.dispatchEvent(e);"+
                    "})()");

给了我这个错误:这个错误还会导致网站由于某种原因崩溃,因为这个错误会向我的日志发送垃圾邮件。

02-17 11:28:13.368 6502-6502/com.example.okke.testwebapp03 I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot read property 'dispatchEvent' of undefined", source:  (1)
02-17 11:28:13.424 6502-6502/com.example.okke.testwebapp03 W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 6502
02-17 11:28:14.270 6502-6502/com.example.okke.testwebapp03 W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 6502
02-17 11:28:14.316 6502-6502/com.example.okke.testwebapp03 W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 6502
02-17 11:28:14.344 6502-6502/com.example.okke.testwebapp03 W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 6502
02-17 11:28:14.389 6502-6502/com.example.okke.testwebapp03 W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 6502

第三种方法:

mWebView.loadUrl("javascript: var y = document.querySelector('#login-form [type=" + "'" + "submit" + "'" + "]'); y.click();");

给我这个错误:

 02-17 11:31:01.786 8790-8790/com.example.okke.testwebapp03 I/chromium: [INFO:CONSOLE(1)] "Uncaught SyntaxError: missing ) after argument list", source:  (1)

编辑---

我使用以下代码启用了 JavaScript 和 DomStorage:

    WebSettings settings = mWebView.getSettings();
    settings.setDomStorageEnabled(true);
    settings.setJavaScriptEnabled(true);

最佳答案

您必须在 WebView 设置中启用 JavaScript,请参阅此答案:https://stackoverflow.com/a/5089694/4310905

关于javascript - WebView,JavaScript 单击按钮的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35454176/

相关文章:

javascript - RxJS 5 任务队列,如果任务失败则继续

java - 找到两个链表的合并节点?

java - 使用 vlcj 流式传输视频文件

android - Android 在哪里存储关机日志?

android - 无法使用 Fovea 购买插件从 Google Play 商店检索产品

javascript - d3.js 在 v4 中设置属性

asp.net - JavaScript 中的奇怪行为

javascript - 我应该在文件加载器中指定什么选项,以便开发服务器正确加载文件

java - 如何在 Spring Boot 应用程序中关闭客户端 sse 连接

android - 如何区分谷歌眼镜和其他蓝牙设备?