3

I am trying to click a button on a website I am loading with a WebView with JavaScript but so far I haven't had any luck.

I have tried 2 new methods to press the button, both without succes:

first method:

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

second method:

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

errors:

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)

This is currently my code to execute all my JS functions:

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);"+
          "})()");

These are the errors I am getting now:

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

I have tried multiple methods:

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

That gave me this error:

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

Second method:

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

Gave me this error: Also this error makes the website crash for some reason, since this error spams my log.

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

Third method:

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

Gave me this error:

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

I have JavaScript and DomStorage enabled with this code:

    WebSettings settings = mWebView.getSettings();
    settings.setDomStorageEnabled(true);
    settings.setJavaScriptEnabled(true);
Okke Trommelen
  • 165
  • 1
  • 3
  • 12

2 Answers2

0

You have to enable JavaScript in your WebView settings, see this answer: https://stackoverflow.com/a/5089694/4310905

Community
  • 1
  • 1
and_dev
  • 3,723
  • 1
  • 20
  • 28
0

Try it with this

mWebView.loadUrl("javascript: document.getElementsByName('Submit')[0].click()");
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • 1
    Just gave me this error ' 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 Feb 17 '16 at 11:07