0

I have an application that I use to play text based games. This application has scripting built into it it so you can create your own plugins. It supports various scripting languages such as JScript, VBScript, and LUA. What I want to do is create a plugin that is capable of logging into an account on a website and then perform various actions, like click a button or submit information. I've done a lot of research into this and I've made some degree of progress, but I keep running into issues. I'm not sure if what I'm trying to accomplish is even possible in the way I'm trying to do it.

So far I've been able to open up the browser and navigate to a page using CreateObject.

function WindowLoad
     IE.Document.getElementByID("user").value = "testacct"
end function
Dim IE 
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = 1  
IE.navigate "http://www.mudconnect.com/SMF/index.php?action=login2"
Set IE.Window.OnLoad = GetRef("WindowLoad")

I'm now trying to figure out a way to fill the input fields with my information. At first I tried to use IE.Document.getElementByID(), however it was displaying this error message: Object required: 'Document.getElementByID(...)'

After doing some research I found out that this could be because it's trying to find the element before the page is loaded. So I decided to try the OnLoad method. However now I can't seem to find any way to reference the "Window." I've tried IE.Window.Onload, IE.Document.Window.Onload, and several other references, but it keeps saying: Object doesn't support this property or method: 'Window'. Anyone have any suggestions for me?

I've also tried:

Do While (IE.Busy) 

 Loop
 IE.Document.getElementByID("user").value = "testacct" 

I've managed to automatically fill out the form and submit it. My next goal was to be able to navigate while logged in and perform other actions such as voting. I wanted to set up a timer in my client to vote once a day, in case I forget to do it. However, the browsers behavior seems to be erratic once I try to perform another navigate. Here's my updated code:

dim pause
Dim IE 
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = 1  
IE.navigate "http://www.mudconnect.com/SMF/index.php?action=login"
while IE.Busy
   pause = pause + 1
wend
world.note pause
IE.Document.Forms(1).Action = "http://www.mudconnect.com/SMF/index.php?action=login2"
IE.Document.Forms(1).Elements(0).value = "myUserName"
IE.Document.Forms(1).Elements(1).value = "myPass"
IE.Document.Forms(1).Submit
while IE.Busy
    pause = pause + 1
wend
world.note pause
IE.navigate "http://www.mudconnect.com/cgi-bin/vote_rank.cgi?mud=Legends+of+the+Jedi"
while IE.Busy
    pause = pause +1
wend
world.note pause
IE.Document.Forms(0).Submit

It doesn't appear to be navigating to the new page for some reason. It somehow gets sent somewhere else on the website. Another problem seems to be that it doesn't log me in if I set up a navigate to go somewhere else. I've spent hours reading up on the IE object documentation, but I can't seem to figure out what the issue is. I'm also curious if the IE.Document values get updated whenever you navigate to a new URL?

I solved this part of the problem. The best advice I have for anyone dealing with these particular issues is to pay close attention to the number of forms and elements on a page. View each form as a slot in an array of "forms." Then within each of these forms there is also an array of "elements." A lot of my problems were the result of not identifying the proper "form" or "element" within the array.

However, I still was unable to make this script login AND navigate afterwards. The solution I developed in light of this involved creating a new IE object and having it finish the task for me. It would be more elegant to have the IE object navigate a second time, but I was unable to figure that out. For now I'm going to flag this as solved, since I have found a way to make it work, but I am curious if there is a way to solve the issue I was having.

Catalyst
  • 59
  • 1
  • 8
  • Look at the [Busy](http://msdn.microsoft.com/en-us/library/aa752050(v=vs.85).aspx) property. You can just busy-wait (i.e., have an empty loop while IE.Busy). – Casey Sep 06 '14 at 19:05
  • I've actually tried that already, but it still throws the "Object required: 'Document.getElementByID(...)'" error – Catalyst Sep 06 '14 at 19:09
  • Take it out of "WindowLoad" and put it after the busy-wait and it should probably work. – Casey Sep 06 '14 at 19:13
  • Same result.. I tried it again using this: Do While (IE.Busy) Loop IE.Document.getElementByID("user").value = "testacct" – Catalyst Sep 06 '14 at 19:16
  • That's not what I mean... While IE.Busy | Thread.Sleep(1000) | End While | IE.Document.getElementById("user") [...] – Casey Sep 06 '14 at 19:19
  • That's not real VBScript since I've never used it but I hope you get the idea. – Casey Sep 06 '14 at 19:19
  • I'm getting an object required for Thread now. – Catalyst Sep 06 '14 at 19:24
  • Yeah, Thread.Sleep isn't VBScript, as it turns out... have a look at my answer. – Casey Sep 06 '14 at 19:28

1 Answers1

1

The easiest thing is to wait on the Busy property.

Dim IE 
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = 1  
IE.navigate "http://www.mudconnect.com/SMF/index.php?action=login2"
While IE.Busy
  WScript.Sleep 1000
Wend
IE.Document.getElementsByName("user")(0).value = "testacct"

I believe WScript.Sleep will work for sleeping but if not look at this answer. I've never used VBScript but the principle is the same regardless of language.

Another problem, which I noticed when I went to that page, is that no element has the id "user." There's an input box with the name user but that's different. getElementsByName returns an array though.

Community
  • 1
  • 1
Casey
  • 3,307
  • 1
  • 26
  • 41
  • I actually tried WScript.Sleep originally, but I don't think it is supported by the vbscript embedded in the application I used. – Catalyst Sep 06 '14 at 19:29
  • @user3479482 Use your imagination; it doesn't really matter what you do inside that block so much. – Casey Sep 06 '14 at 19:33
  • Indeed. I didn't think that it did. In fact I've thrown a random statement in already previously, but for some reason it still says the same message: Object required: 'Document.getElementByID(...)' – Catalyst Sep 06 '14 at 23:00
  • I just read your edit. The method I was trying to access the input box seems to be the problem. Thanks man! I'm one step closer to accomplishing my task :D. – Catalyst Sep 06 '14 at 23:12
  • You've definitely helped me get through a big part, but I still can't seem to log into the website. – Catalyst Sep 07 '14 at 02:01
  • @user3479482 Have you tried using Fiddler or something to see what you're actually sending through? That might give you a hint. – Casey Sep 07 '14 at 02:52
  • I really appreciate your help man. I'll have to look into using Fiddler. I've managed to log in now by referencing the Form part of the Document. However, I'm still running into a few issues. – Catalyst Sep 07 '14 at 18:11