1

Hi im trying to get my program to hit a submit button after entering some strings into a few text fields, but i cant get it to work.

Im using the following code:

web.loadUrl("http://ta.yrdsb.ca/yrdsb/");

        web.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
               String username=MainActivity.usernameString;
               String password=MainActivity.passwordString;

               view.loadUrl("javascript:document.getElementsByName('username')[0].value = '"+username+"'");
               view.loadUrl("javascript:document.getElementsByName('password')[0].value = '"+password+"'");
               view.loadUrl("javascript:(function() { document.getElementsByName('submit')[0].click()})()");

            }
         });

        }

Adding my strings to the text fields is working, but the last line where i try to click the submit button is not. Any help would be greatly appreciated. Thanks

Noob
  • 534
  • 1
  • 8
  • 16
  • The extra set of parenthesis looks suspicious to me, but to be honest, it's been a number of years that I have even touched Javascript. – Stephan Branczyk Jan 18 '14 at 22:15

1 Answers1

0

Looks like a typo Change this:

"javascript:(function() { document.getElementsByName('submit')[0].click()})()"

to this:

"javascript:(function() { document.getElementsByName('submit')[0].click()})"
Jim
  • 10,172
  • 1
  • 27
  • 36
  • Thanks, but still no change. Nothing is being submitted. – Noob Jan 18 '14 at 22:22
  • post your javascript - what is the form supposed to do? – Jim Jan 18 '14 at 23:39
  • Its this site: https://ta.yrdsb.ca/yrdsb/ Im trying to make an app to log into it. – Noob Jan 19 '14 at 00:56
  • you are using the "getElementsByName" - you should problaby use "document.forms["loginForm"].submit();" – Jim Jan 19 '14 at 01:06
  • 1
    Thanks again, but unfortunately its still not working. This is what Im using: view.loadUrl("javascript:(function(){document.forms['loginForm'].submit();})"); I really dont understand why thats not working. The page has a line declaring the id of the form and this code seems to match everything perfectly. – Noob Jan 19 '14 at 02:32
  • try doing all of it with one call ... and maybe use javascript bridge to pass debug variables back: http://stackoverflow.com/questions/7636409/android-webview-java-javascript-bridge – Jim Jan 19 '14 at 16:58