我正在尝试点击我使用带有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)
EDIT ---
我使用此代码启用了JavaScript和DomStorage:
WebSettings settings = mWebView.getSettings();
settings.setDomStorageEnabled(true);
settings.setJavaScriptEnabled(true);
答案 0 :(得分:0)
您必须在WebView设置中启用JavaScript,请参阅以下答案:https://stackoverflow.com/a/5089694/4310905
答案 1 :(得分:0)
尝试使用此
mWebView.loadUrl("javascript: document.getElementsByName('Submit')[0].click()");