Having trouble getting vbscript to recognize an input area - vbscript

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

Related

Using VBS script to fillout an online form

So I have a really annoying set of forms at work that I have to fill out daily and spend about 20 minutes doing so, I was looking to script this and have a basic sendkeys method that works but I wanted to improve this.
Dim WshShell, objIE, ElementCol
Dim LinkHref
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Webpage Title"
Set objIE = CreateObject("InternetExplorer.Application")
wshShell.AppActivate objIE
Do While objIE.Busy
wscript.sleep 100
Loop
Set ElementCol = objIE.document.getElementsByTagName("a")
For Each Link In ElementCol
If Link.innerHTML = "*Specfic text" Then
Link.Click
Exit For
End If
Next
So I get an error on the Set ElementCol line that says null thats my first issue
For the *specific text is this allowed? Basically I know the text of the links will be Hello and world but could pop in like 1) Hello and 2) World or 1) World 2) Hello
I also want to after the click run a different .vbs script that fills out the form after that .vbs script completes it returns back to this initial page where I want to continue on this script to the next link.
Thanks a ton for any help!

how to use SendKeys method in vb script to select everything on page?

I am trying to run a vbscript that will load up a html page in chrome and then do control+a which will select everything on the page, with the ultimate view to copy it and then paste it into excel.
this my script so far:
Set WshShell = WScript.CreateObject("WScript.Shell")
Dim iURL
Dim objShell
iURL = "C:\Users\Aasfasf\AppData\Local\Temp\TD_80\hpqc\52136023e30\****\121200.html"
set objShell = CreateObject("WScript.Shell")
objShell.run(iURL)
WScript.Sleep 1500
WshShell.SendKeys "^a"
when I run it loads up the html page, but i dont think the control+a command is working as nothing is selected.
Two suggestions:
The focus is probably not on the content in chrome. Try sending a Tab key press a couple of times first to see if that fixes it.
A better approach to this might be to just read the contents of the file, then delete all the markup from the document using regex.
Excel can just load your web page without needing any help from you.
Alt + D, D, W

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

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.

Automate 5 Firefox Tab

I am looking to automate our reporting. We currently have 5 Tabs in Firefox that we refer to for reporting. Each Tab has a unique url that changes each morning. We then have to each morning update these URLs (from an excel speadsheet). I would like to automate this. I do have a few constraints on this though. I cannot open a new tab for each new url (so basically it needs to be a url replace in each tab). I have looked into selenium but cannot get it to work
http://www.makeuseof.com/tag/using-vba-to-automate-internet-explorer-sessions-from-an-excel-spreadsheet/
and
http://www.makeuseof.com/tag/how-to-automate-firefox-or-chrome-with-vba-and-selenium/
So I was wondering if it could be done another way either using pure VBA or a batch file?
You need to add a reference to Microsoft Internet Controls and Microsoft Shell Controls and Automation. See screenshot below.
Code:
Sub Work_With_Open_IE_Instance()
Dim objShell As Shell
Dim objIE As InternetExplorer
Dim objWin As Object
Set objShell = New Shell
For Each objWin In objShell.Windows
If TypeName(objWin.Document) = "HTMLDocument" Then
Set objIE = objWin
'~~> This will display the URL of the IE which we will use to bind
Debug.Print objIE.LocationURL
'~~> Example to bind
'~~> Change URL to your existing URL
Select Case objIE.LocationURL
Case "https://www.google.com"
With objIE
.Refresh
'
'~~> Rest of the code
'
End With
Case "http://in.msn.com"
With objIE
.Refresh
'
'~~> Rest of the code
'
End With
End Select
End If
End If
Next objWin
End Sub
The Select Case is just for demonstration purpose. If you want to work with them one after the other then don't use Select Case. Use If/EndIf one after the other.

Vbscript to open internet explorer application and get a response text

I want to open an Internet Explorer page using vbscript, currently my script is as follows
Dim IE
Dim MyDocument
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = 0
IE.navigate "https://google.com"
This works fine. The problem is that I need to get the response of my webpage (google.com) i.e its contents in string format
I know it will be mostly gibberish but I require it for what I am working on
Thanks
You're almost there already. You just need to wait until the page has finished loading
While IE.ReadyState <> 4 : WScript.Sleep 100 : Wend
then you can access the page text like this:
WScript.Echo IE.document.body.innerText
or the page HTML like this:
WScript.Echo IE.document.body.innerHTML

Resources