Can VBScript automatically log into a website and perform actions via CreateObject(InternetExploerer)? - vbscript

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.

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.

Related

Blocking code in custom message box in VB6

In my vb6 project, I created my own msgbox using a form, due to difficulties in changing
the font/language of in-built msgbox. A search with google gave the idea of own msg-
box rather than trying msgbox of VB. Now the problem is : - when the user exits the
program, 3 options are given: to close, to restart and to cancel exit. The user need not
again go through the process of giving password etc in restart option. If I give cancel =
true in the QueryUnload event, then 2nd option does not work, 3rd option works. If
cancel = true is not given, 2nd option works, but 3rd option does not. It appears that
the main form does not get unloaded if cancel = true. Unless & until the main form
unloads, the program will not work with the fresh data to be given by the user in the
initial Form. Since the code after "msgbox.show" depends on options, it is not possible
to write that code in the same sub, not even in the same form code. Is there any way
to stop the subsequent code after "msgbox.show" and continue the same after getting
option? (like in the in-built msgbox of VB.) I am not an expert in VB, so please correct
if I made some mistake; also help with advice/suggestions.
EDIT:- [Extended explanation]
The 3 forms in my project:
Initial form for password, data etc. This is input Form for user.
Main Form. This Form shows the results after process of input.
frmMsgBox. This is a custom msgbox created using a form.
Main Form code portion. Code for closing the program:
Private Sub Form_QueryUnload(Cancel as.......)
cancel = True
frmMsgBox.Label1.caption = Do you wish to 1.Exit 2.Restart
3.Cancel the exit?
frmMsgBox.Show
End sub
(The above msgbox is almost like an in-built msgbox in VB with
vbYesNoCancel buttons) The message is in regional language,
which was the main reason forced me to use my own msgbox.
After MsgBox appears, the user selects one of the above options
using 3 commandButtons placed in that Form. The code after
clicking these buttons is written in the code portion of frmMsgBox:
Command1_Click 'This is for Exit from the Program.
All Forms.unload, All forms set to nothing, end.
Command2_click 'This is for restarting the Program.
Unload Main Form, set to nothing
Load Initial Form
Initial Fom.show
frmMsgBox.Hide
Command3_Click 'This is for cancelling the exit request.
Main Form.Show
frmMsgBox.Hide
With the above code, I have no problem with options 1 & 3,
i.e; to exit from the program or to start. The frmMsgBox hides,
the initial form shows - these are OK, but the main form does
not unload nor it is removed from memory. Because of this,
whatever new data is given by the user in the initial form now
is not being processed, the main form is struck with the old results.
If cancel = true is removed from the code above, Options 1 & 2
are OK, but option 3 does not work. Then the Main Form loses
all its results (all labels, texts etc in that Form turn blank.)

Create VBScript script detect iexplore

I want to create a VBScript script to detect Internet Explorer opening page and opening new tab in the same window. For example, when I manually open Internet Explorer at www.google.com the VBScript will do:
detect and open new tab in same window with www.example.com
but do this only once
I tried with this code:
Set wshShell = CreateObject("WScript.Shell")
Do
page1 = wshShell.AppActivate("Blank page - Internet Explorer")
If page1 = True Then
Set page2 = CreateObject("InternetExplorer.Application")
page2.Navigate "http://www.example.com", CLng(navOpenInNewTab)
End If
WScript.Sleep 500
Loop
For one thing, you must use the existing Internet Explorer instance instead of creating a new one. Unfortunately the way one might expect to be able to do that (GetObject(, "InternetExplorer.Application")) does not work for Internet Explorer COM objects. AppActivate also doesn't help here, because it doesn't return a handle to the application object, which you need to invoke its methods. Instead you need to do it this way:
Set app = CreateObject("Shell.Application")
For Each wnd In app.Windows
If InStr(1, wnd.FullName, "iexplore.exe", vbTextCompare) > 0 Then
Set ie = wnd
Exit For
End If
Next
If you want to select an instance that has a particular page opened, you can for instance check the title of that page to make the selection:
wnd.Document.Title = "something"
or
InStr(1, wnd.Document.Title, "something", vbTextCompare) > 0
Your second problem is that VBScript doesn't recognize the symbolic constant navOpenInNewTab. You must either use the numeric value (as defined in the BrowserNavConstants enumeration):
ie.Navigate "http://www.example.com", CLng(2048)
or define the constant yourself first:
Const navOpenInNewTab = &h0800&
ie.Navigate "http://www.example.com", navOpenInNewTab
Note that you must use hexadecimal notation with a trailing ampersand here, because the value must be a Long and you must use literals in Const definitions. Expressions like calling the function CLng are not valid.
Alternatively you can open a URL in a new tab by omitting the second parameter of the Navigate method entirely and instead providing the value "_blank" for the third parameter:
ie.Navigate "http://www.example.com", , "_blank"
However, I'm not certain that this would always open a new tab in the current window (might depend on the browser's tab settings), so I'd recommend using the second parameter (flags) as described above.

Wait until a 'Click' on a WebButton of type submit is completely finished

I am trying to automate JIRA post configuration using VBScript. After database configuration page when I click on next button it takes several minutes to go on next URL. I want my VBScript to wait until this next button process is finished and then go to next URL.
Right now, after the next button I have made the script sleep till 2 minutes and then navigated it to next page.
But I need some replacement to WScript.Sleep in my code. Since this time will differ on every machine.
Following is the code I'm using right now:
IE.Document.getElementById("jira-setup-database-submit").Click
WScript.Sleep 120000
If (fso.FileExists("C:\Program Files\Atlassian\Application Data\JIRA\dbconfig.xml")) Then
Call URL3
Else
ie.Visible = 1
MsgBox("Please enter valid Database Credential ")
End If
This will cause VBA to effectively pause until the browser returns that it's ready and has finished loading.
Do While (IE.Busy Or IE.READYSTATE <> 4)
DoEvents
Loop

Need VBScript Code to toggle between two Webpages to copy content from one page and paste to another webpage

I am need of VBScript code to paste the content from one IE Explorer WebPage window to another IE Explorer WebPage window.
Currently i know the Process ID.
However I am not able to switch from one webpage to another. Or you can say my control does not move from one page to another.
#shellbye
Function Bmc(abc,xyz)
Set objIE = CreateObject("InternetExplorer.Application")
Call objIE.Navigate("// SOME APLLICATION") ' #opened the Web applcation
'# LOGGED INTO THE APPLCATION SUCCESSFULLY suing the VBScript
For i = 0 To objIE.Document.links.Length - 1
if objIE.Document.links(i).innerText ="Create a New Case" Then
Do Until objIE.ReadyState = PAGE_LOADED : Call WScript.Sleep(100) : Loop
objIE.Document.links(i).click '## THIS OPENS A NEW Window as link is an URL
Exit For
End If
End Function
In above, I am able to Open the new window after clicking the link i.e. at Step ##
Now I want to copy the content abc and xyz passed in the function Bmc(abc,xyz) to New Window opened through this.
However I am unable to move or us can say switch control to new window.

Having trouble getting vbscript to recognize an input area

I'm very new to all this, but I'm trying to write a script to quickly fill out a form we use for helpdesk issues in an extremely busy area. I've tried various ways to get text into the field and scoured the internet looking for the correct way to do it, however either I'm searching for the wrong keywords or I'm blind.
the specific field I'm trying to manipulate is below:
input name="custom_ticket_form_field_2" type="text" style="width: 400px" value=""
The area I'm having trouble with:
Option Explicit
Dim objIE : Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate "about:blank"
objIE.Visible = true
objIE.Navigate "http://xxxx/xxxx"
Do Until objIE.ReadyState = 4
WScript.Sleep 500
Loop
objIE.Document.getElementsBy**????**("custom_ticket_form_field_2").value = "laptop"
No matter how I try to identify(????) the "custon_ticket_form_field_2" I can't seem to get the text to end up there. Thanks for any suggestions!
You want:
objIE.Document.getElementsByName("custom_ticket_form_field_2").item(0).value

Resources