2

I may be missing a very basic thing, but i just dont understand why this isnt working.

I am looking to fill out some web forms based on some user input.

I have already written this in vbscript where its working flawlessly. Now i want to import it to VB.

My 2 main issues atm.

Before, i would have a piece of code:

   For Each wnd In CreateObject("Shell.Application").Windows
  If InStr(1, wnd.FullName, "iexplore.exe", vbTextCompare) > 0 Then
    Set IE = wnd
    Exit For
  End If
Next

For deciding what Internet Explore window to use. However, if i use this form in VB, i miss out on pretty much any type of event handling. I cant use IsBusy, i cant use DocumentCompleted or anything.

So instead i have to declare IE as a browser.

Dim IE As New WebBrowser

Which seems fine.. Except.. Now i cant navigate anywhere, no browsers are created, theres nothing happening. So when i run my code:

IE.Navigate("www.awebwithusernameandpasswordform.org")
                IE.Visible = True



                Helem = IE.Document.GetElementById("username")
                Helem.Value = "xxxx"
                Helem = IE.Document.GetElementById("password")
                Helem.Value = "xxxx"
                Helem = IE.Document.Forms("signupForm")
                Helem.Submit()

It just fails at the element "Username" because it never gets to navigate to the website.

It like it just ignores the IE.Navigate. I have tryed running a new process with process.start() but that doesnt work either, it still isnt opening a webpage for me or anything.

Secondly, i am having major issues having vb checking when a document is completely loaded. In VBS i used

Do while ie.readystate <> 4 or ie.busy or ie.document.readystate <> "complete" 
wscript.sleep 500 
Loop 
Do until ie.document.readystate = "complete" 
wscript.sleep 500 
Loop

And that works like a charm.

In VB, ie.document.readystate is not recognised, and i cant seem to get DocumentCompleted event handling to work. I have attempted several thing from this website, but either they just make everything hang, or it just doesnt work for various reasons. I have attempted the 2 solutions from How to wait until WebBrowser is completely loaded in VB.NET? but they both just make the script hang.

Any tips would be greatly appreciated..

Community
  • 1
  • 1
Gematria
  • 105
  • 1
  • 14
  • it needs to be declared WithEvents: `Private WithEvents wb As New WebBrowser` or drop one on the form from the toolbox – Ňɏssa Pøngjǣrdenlarp Jan 07 '15 at 00:59
  • 1
    I think the reason you are running into problems is that Webbrowser is a control. Check [link1](http://www.vbtutor.net/vb2008/vb2008_lesson19.html) and [link2](http://www.vbforums.com/showthread.php?384076-Webbrowser-Control-Tip-and-Examples). – Mathemats Jan 07 '15 at 02:48
  • Duplicate of [How to wait until WebBrowser is completely loaded in VB.NET?](http://stackoverflow.com/questions/3275515/how-to-wait-until-webbrowser-is-completely-loaded-in-vb-net) – Bjørn-Roger Kringsjå Jan 07 '15 at 06:46
  • Plutonix ill give that a look. Thanks. Mathemats ill give those a look too... Have never done VB before, just figured it would be easier to implement the VBS i already had, rather than doing it in C#, which is the only other language i have some experience in. Bjørn-Roger: Then i guess you missed around half my post, which isnt about the documentcompleted event, but about just getting a browser to load a URL. – Gematria Jan 07 '15 at 08:25

1 Answers1

0

What is happening is that your code runs way faster than the browser can navigate, meaning that you try to get elements before it has loaded any, as Bjørn-Roger Kringsjå tried to say.

What you need to do is to wait between .Navigate and .GetElementById :

IE.Navigate("www.awebwithusernameandpasswordform.org")

'WAIT BEFORE COMPLETION

'Continue
Helem = IE.Document.GetElementById("username")
Helem.Value = "xxxx"

However, this is really tricky to do in-line and is not recommended. It is much better to use the DocumentCompleted event handler for the browser control to run code after a page is loaded. Hope this clears it up enough.

Viktor Ek
  • 353
  • 1
  • 13