WebView,JavaScript点击按钮出现问题

3

我正在尝试使用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);

你想点击某个东西的网站链接在哪里?因为根本没有... - Murat Karagöz
它在之前已经加载了,我还使用loadUrl方法填充了网站上的2个文本框。所以我知道那部分是有效的。 - Okke Trommelen
你确定使用 document.getElementsByName('Submit')[0] 能获取到正确的元素吗? - Murat Karagöz
不是真的,因为我不太懂JavaScript。 - Okke Trommelen
http://remotepressure.com/ - Okke Trommelen
2个回答

0

我已经启用了JavaScript和DomStorage,代码如下: WebSettings settings = mWebView.getSettings(); settings.setDomStorageEnabled(true); settings.setJavaScriptEnabled(true); - Okke Trommelen
@OkkeTrommelen,你找到任何解决方案了吗? - Mubashir Murtaza

0

使用这个试试

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

1
刚刚给我这个错误: ' 02-17 12:06:19.476 12094-12094/? I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot read property 'click' of undefined", source: (1)' - Okke Trommelen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接