Log on webpage

noclass1980

Active Member
Licensed User
Longtime User
Hi, I'm trying to open a webpage and enter the user name programmatically. I have used code based on threads in the forum. It successsfully opens the webpage but I can't enter the username or get the focus onto the username input box. Any suggestions?
 

noclass1980

Active Member
Licensed User
Longtime User
webpage

Is this page publicly accessible? If yes then please post a link to it.

it is sort of. Its my company webmail page so I don't think I can post it in an open forum. I could send it privately though.
 
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
Use a tool such as browser development tool such as FireBug to understand what happens when you submit the form.

thank you, I will try it over the weekend.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Load the webpage in a desktop browser and look at the webpage source.
(Using Firefox that is Ctrl + U).

Now locate the <form> element that is the login form.
Has the form element got an id attribute, such as <form id="myForm">?
Can you identify the username <input> element?
Has it got a name attribute such as <input name="username">?

If the form element has an id and the username input element has a name then you can:

1) Wait for the webpage to load - use the WebView PageFinished (Url As String) event.

2) Execute some javascript in the (loaded) webpage to set the text value of the username input.

For example your webpage login form may be like this:

PHP:
<form id="myForm">
    <input name="username">
</form>

So to complete the username input element of the form you'd execute javascript:

B4X:
document.forms.myForm.username.value="fubar";

Can you look at the webpage source code and post the entire <form></form> HTML?

Martin.
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
The easier way is as Erel has said, use either firebug or I prefer live http headers with Firefox then logon to the company email, in live http headers there will be a 'post' which will have the url and data required to send your own post using httputils2, you may also have to set some cookie info

Sent from my HTC One X using Tapatalk 2
 
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
webpage logon

Hi, thanks for the reply. The webpage is attached. There doesn't seem to the form id that you suggest but I think the information I need to access is in Table 6 so is the format for the java script something like

document.table6.username.value="myusername"

is it possible to partially set the password in the app and then allow the user to complete it as this adds to the security a bit?

thanks in advance.
 

Attachments

  • Webpagescript.txt
    9.8 KB · Views: 157
Last edited:
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
thanks, how do I do the live headers?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Here's the parts of the HTML that are relevant:

PHP:
<form action="/CookieAuth.dll?Logon" method="post" id="logonForm" autocomplete="off">
<input class="txt" id="username" name="username" type="text" />
<input class="txt" id="password" onfocus="g_fFcs=0" type="password" name="password" />

And a simple b4a project that should enable you to test interacting with the form once the webpage has completed:

B4X:
Sub Process_Globals
End Sub

Sub Globals
   Dim LoginPageUrl As String
   Dim WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   LoginPageUrl="put your webpage url here"
   WebView1.Initialize("WebView1")
   Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
   WebView1.LoadUrl(LoginPageUrl)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub WebView1_PageFinished (Url As String)
   Log("WebView1_PageFinished: "&Url)
   If Url=LoginPageUrl Then
      Dim Javascript As StringBuilder
      Javascript.Initialize
      Javascript.Append("javascript:")
      Javascript.Append("document.forms.logonForm.username.value='foo';")
      Javascript.Append("document.forms.logonForm.password.value='bar';")
      Javascript.Append("document.forms.logonForm.submit();")
      WebView1.LoadUrl(Javascript.ToString)
   End If
End Sub

So if you update LoginPageUrl with the URL of your webpage and then change the username and password values in WebView1_PageFinished you should (might!) see the page load and submit itself and leave you logged in.

You can comment out any of the lines that begin Javascript.Append("document.forms.logonForm, be sure to leave the Javascript.Append("javascript:") in place though.
So give it a try and if it works you can experiment with only partially completing the password field in the form.

Not sure what you mean by 'live headers', are you referring to cookies? If so the WebView will handle this for you automatically.

Martin.
 
Last edited:
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
Logon partially working

Hi, thank you for the help so far. I've tried your code and it successfuly enters the username but will not put the password into the password box. I tried re-initialising the javascript and sending the password after sending just the username but that didn't work. Any suggestions how I might be able to send the password as well? Thanks
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
:sign0161:

Ooops a mistake in the javascript i posted, i missed out .value from the line that sets the password.
This is the corrected code:

B4X:
Sub WebView1_PageFinished (Url As String)
   Log("WebView1_PageFinished: "&Url)
   If Url=LoginPageUrl Then
      Dim Javascript As StringBuilder
      Javascript.Initialize
      Javascript.Append("javascript:")
      Javascript.Append("document.forms.logonForm.username.value='foo';")
      Javascript.Append("document.forms.logonForm.password.value='bar';")
      Javascript.Append("document.forms.logonForm.submit();")
      WebView1.LoadUrl(Javascript.ToString)
   End If
End Sub

I just tested it by loading the webpage in your earlier post - added the webpage to the Files tab and used this to load it:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   LoginPageUrl="file:///android_asset/login_page.html"
   WebView1.Initialize("WebView1")
   Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
   WebView1.LoadUrl(LoginPageUrl)
End Sub

Username and password both completes and the page errors out when it tries to submit itself as it can't find the URL that the form submits to (CookieAuth.dll?Logon).
If you test with the online webpage i expect it will all work...

Martin.
 
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
Perfect! thank you very much!
:sign0188:
 
Upvote 0
Top