WebView,JavaScript点击按钮的问题

WebView, problems with JavaScript clicking button

本文关键字:按钮 问题 JavaScript WebView      更新时间:2023-09-26

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

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

第一种方法:

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

试试这个

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