Sending Keystrokes In Webview ?

RichardN

Well-Known Member
Licensed User
Longtime User
I want to automate a per-user login to an intranet to access and format some specific html pages.

The login page protects only low-level information and I would like to emulate text entry of username, password and 'enter' button. Can I set focus to a field and send keystrokes to a WebView tabbing between fields once the page is loaded?

The login page contains some JS code to facilitate timeout and password retries protection using some server-side activity so to emulate that on the device would be a major PITA.

Something like the old MS.... Sendkeys() ???? Thanks
 
Last edited:

admac231

Active Member
Licensed User
Longtime User
I did this using the WebViewExtras library.

In the WebView1_PageFinished sub you can execute javascript on the page.

For example:
B4X:
webviewExtras1.execJS(WebView1,"document.forms['logout_form'].submit();")

Will simulate a Submit button click on the form called "logout_form".
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
Thanks, that sounds just the job.

... Hope you're not wasting too much time in 'The Bread' :sign0098:
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
Had a mate doing IT in Dundee long since.... The Bread was the student dive on Constitution Road.

Maybe somebody burnt it down.... It needed it!


I'm having some difficulties with executing JS. Does this Lib need B4A > Ver 1.5 by any chance ?
 
Last edited:
Upvote 0

admac231

Active Member
Licensed User
Longtime User
I always thought that was a bakery! It's been closed ever since I moved up here (2 years ago).

I only have 1.5 however I haven't used WebViewExtras. When I used it it was JSInterface.

What's the problem you're having?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi there.

WebViewExtras should work with any recent version of B4A - you are compiling with Android 8 are you?
(See Tools menu > Configure Paths then check the path to android.jar).

Can you load the login webpage in a desktop browser then view the page source?
In Firefox Ctrl U displays the page source.

Look for the login form in the page's HTML.
Has the form got an id attribute?
Have the username and password input tags got name attributes?

If so you can wait for the page to load (PageFinished event) and execute some javascript to populate the username and password inputs and then submit the form.

Say this is your form in the HTML:

B4X:
<form id="myForm">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" value="Submit" />
</form>

This is the javascript that you need to execute:

B4X:
document.forms.myForm.username.value='MyUserNameHere';
document.forms.myForm.password.value='MyPasswordhere';
document.forms.myForm.submit();

So in your B4A code you need to do something like...

B4X:
Sub WebView1_PageFinished (Url As String)
   If Url="url_of_the_login_page" Then
      Dim Javascript As String
      Dim MyWebViewExtras As WebViewExtras   '   you could make this a global variable if you use it elsewhere in your code
      Javascript="document.forms.myForm.username.value='MyUserNameHere';document.forms.myForm.password.value='MyPasswordhere';document.forms.myForm.submit();"
      MyWebViewExtras.executeJavascript(WebView1, Javascript)
      Log("Form submitted")
   Else
      Log("Page_Finished "&Url)
   End If
End Sub

Martin.
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
Martin, yes to all the requirements above. (I must confess to being a JS numpty)

A question however, do successive executeJavascript() commands execute asychronously, or do I need to ensure that the whole script is wrapped in one executeJavascript() command ? Clearly you need to ensure the fields get populated before executing Submit() otherwise an error gets trapped by the form.

The code I am having a problem with is....
B4X:
Sub mnuLogin_Click

   Dim webviewExtras1 As WebViewExtras 
   Dim Js(3), Jscript As String 

   Js(0) = "document.forms.login_form.password_username.value = '" & Username & "';"
   Js(1) = "document.forms.login_form.password_password.value = '" & Password & "';"
   Js(2) = "document.forms.login_form.submit();"
   
   Jscript = Js(0) & Js(1) & Js(2)
   
   webviewExtras1.executeJavascript (WebView1,Jscript)
         
End Sub
If I execute this sub as you see it then the result is nothing... no field population and no submit (as far as I can tell). If however I remove the submit command then the fields are populated correctly but the user is left to to press the submit button themselves.... So it's a mystery!

Without any feedback from the script it is very difficult to debug. The target web-page POSTS username/password or username/PIN+secure id combinations so I think there is something I am missing here.

The offending webpage:
 

Attachments

  • employee_login.txt
    9.8 KB · Views: 343
Last edited:
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi.

I can see nothing obvious with the login page that will stop the javascript submit() from working.

If you use the WebViewExtras executeJavascript method one time with all three javascript statements combined OR three times - once for each statement - it should make no difference.

The problem may be in the form's onSubmit() method though:

B4X:
<FORM AUTOCOMPLETE="off" method="post" onSubmit="return formSubmit(this)" action="https://auth.XXXplc.com/securid-cgi/securid.pl" target="" name="login_form">

When the form is submitted it first calls the javascript function formSubmit().
That function probably checks that the form fields have been correctly populated.
If the form is correctly populated then form submission continues otherwise it is not submitted to the server.

All the login page's javascript seems to be in one javascript file:

B4X:
<SCRIPT src="login.js" language="JavaScript" type="text/javascript"></SCRIPT>

Without that javascript file there's no way i can tell what's happening when the form's submit() method is called.

Can you post a copy of login.js?

If not - it might contain details you don't want to disclose - then feel free to email me with a copy.
I've enabled my Profile settings to allow others members to email me so you should have no problems.

Martin.
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
Hi Martin,

Thanks for looking at that. The login.js is rather more involved with the SecureID login and probably best not circulated.... but thanks anyway. I will live with the user punching the submit button for now.
 
Upvote 0

camolas

Member
Licensed User
Longtime User
Hi,

I cant put the WebViewLogin to work for my email (but it works very well on google) http://auth.clix.pt/login/?request_uri=http://webmail.clix.pt/mail/ the login routine is the same... put my email on "Email" field, put my pass on "Password" field and the press the "Entrar" button.
I cant get clear what is the form name (maybe this is the maine problem) i used "Form1" and "form" i try geting it from html inspect element...
A fast test is to do
B4X:
Javascript.Append("document.forms.gaia_loginform.submit()")
like in gmail to see if the "enter" sign in button is pressed by B4a app.
In my case from my email the button "Entrar" sign in is not pressed...

B4X:
Sub WebView1_PageFinished (Url As String)


   If Url="http://auth.clix.pt/login/?request_uri=http%3A%2F%2Fwebmail.clix.pt%2Fmail%2F" Then
      Dim Javascript As String
      Dim MyWebViewExtras As WebViewExtras   '   you could make this a global variable if you use it elsewhere in your code
      Javascript="document.forms.Form1.registar();"
      MyWebViewExtras.executeJavascript(WebView1, Javascript)
      Log("Form submitted")
   Else
      Log("Page_Finished "&Url)
   End If
End Sub

from html inspect element for the button "Entrar" sign in:

B4X:
<input type="image" name="registar" src="../webmail/images/btn-entrar.gif" alt="Entrar" class="btn1">

Any ideas to solve it?

Thanks

Paulo
 
Upvote 0
Top