Entering Username in HTMLViewer

I am loading a log in web page in an HTMLviewer and would like to automate entering a username and password.

When viewing the source code of the web page, I can see the text field is called “username”.

I tried

WinAuthorize.HTMLViewer1.ExecuteJavaScript ("document.getElementById('username').value = 'username';")

but no luck.

Here is the HTML for the element:

<input name="username" id="username" type="text" class="form-control" value="" autocomplete="off" style="background-image: url(&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAkCAYAAADo6zjiAAAAAXNSR0IArs4c6QAAAnVJREFUWAntVzuL4lAUPokBsVB0t9odK8VCLCal3Q4WbmUpCFv6DyymEmys9gf4B4RlS7GwEWQsBAsL195q2J3KWIiVrz0nJBcTk9yr3jCw7AG5j3PO9325j2OiAFq73X7Y7/ffsft0Op0+05wsUxTlD2K9aJr23Gw2f7txFYv8FxJ/dDtljlHICkU8ukWo9ORhk9ODEIe1yo7nUnH05JgJd3DBpcre8yD9Xly0Au9q/7aAfD4PjUYDqPWzUFegUChAIpEAav0sVAF4901eu/USEaoAL0L3nENANBqFbDYLqVTKHQe3+i6AXBOaPU4mk1Cv1yEej8PxeIR+vw/z+dx03+qzsYNatgLFYtEkp2BVVaFUKrG8W30MIKDDBATEhOpiAqbTKWw2G5OMtmA0GjHiW30MIKCjtFqtk+2ng5ZOp8EwDFiv1/a02Qb5HIGcAV7JNwwZW+8Hrw4BnFypbhRioAidbYFUdAEw/Gf8QO8H7ybA0viF1QEB0cIhmUwGqtUqxGIxM2cymcBwOLzIx1X4JF0A1YxyuWzWkgtGjwlpAiKRCFQqFdB13YPGf0qagFqtBrlczmSiOkJGFZVnKl4Hem+/26hYEfHhcIBerwe73U4IkyS+CEVygpbLJQwGA+h2u7BYLIC2RMSwFmjPeB+/4om8+8NkNpsxzqCXEBaEHZW+VFDEIyb8kLUd5wS8vnkIrc+lb7xg8p//d4jE82L4x5SHcKf/v4CrCxEe1Deq4byVX61WrBBtt1vPcMK6WgAijfFX80Q8m+x0Omcj3+746jNAdQOVG76Qgg7CIKyrBeCVfcVEHQF+0hIK8rEwyqFcwiCsv+R847qxq2vXAAAAAElFTkSuQmCC&quot;); background-repeat: no-repeat; background-attachment: scroll; background-size: 16px 18px; background-position: 98% 50%; cursor: pointer;">

Is there something else I need to do?

The javaScript is correct. Where is that code ?

The page I am trying to work with is here: drchrono

It works for me. Is the page finished loading before you run the javascript?

That was it. Thanks.

Stupid question…
I keep getting an Exception AS OLEException (Exception code 0: Could not complete the operation due to error 80020101., (failed on “execScript”)) when I run my code to fill in a web field…

it’s probably because my webpage has not finished loading… so where should I run the code

me.ExecuteJavaScript("document.getElementById('horde_user').value = 'tom';") ?

I currently have it running in the DocumentComplete event but I keep getting the error…

thanks.

I use something like this :

[code]mWaitElementByID = True

Dim jsSrc As String
jsSrc = “window.status = (document.getElementById(”""+anElementID+""") != null);"

While mWaitElementByID
ExecuteJavaScript(jsSrc)
app.DoEvents(1)
Wend
[/code]

and in the StatusChanged event of the htmlviewer :

If mWaitElementByID then
  mLastResult = newStatus
  if newStatus="true" then
    mWaitElementByID = False
  end if

this way using anElementID as some element in the bottom of the web page, you wait until everything is loaded,
independently of the time it takes (different internet speed)

Thanks Jean-Yves…

I see the second section of code will run in the StatusChanged event of the view but where would I put the first section of code?

[code]mWaitElementByID = True
Dim jsSrc As String
jsSrc = “window.status = (document.getElementById(”""+anElementID+""") != null);"

While mWaitElementByID
ExecuteJavaScript(jsSrc)
app.DoEvents(1)
Wend [/code]

And where do I define/declare the variable mWaitElementByID? What (if anything) do I replace anElementID with?

Thanks again for your help.

mWaitElementByID is a boolean property of the htmlviewer class
first section of the code is (for me) in a method I call WaitforelementbyID (anElementID as string)
I call this method when I have to wait for an element in the bottom of the web page.

I’ll give that a try, thanks Jean-Yves.

Hey Jean-Yves,

Where do I declare the mWaitElementByID variable… am I supposed to subclass the htmlViewer and create the variable there?

yes you can instanciate, or subclass htmlviewer.

Thanks!