Submitting Google Form with VBS - vbscript

<input type="text" class="quantumWizTextinputPaperinputInput exportInput" jsname="YPqjbf"
autocomplete="off" tabindex="0" aria-label="Untitled question"
aria-describedby="i.desc.608035577 i.err.608035577"
name="entry.1790931838" value="" required="" dir="auto" data-initial-dir="auto"
data-initial-value="" aria-invalid="true">
The above is the HTML code of the input box which I would like to automatically fill with VBS.
<span jsslot="" class="appsMaterialWizButtonPaperbuttonContent exportButtonContent"><span class="appsMaterialWizButtonPaperbuttonLabel quantumWizButtonPaperbuttonLabel exportLabel">Submit</span></span>
And the above is the code for the submit button.
On Error Resume Next
Const PAGE_LOADED = 4
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("https://docs.google.com/forms/d/e/1FAIpQLScOmcmtdsrm3RiX7FD9ur2eLPULL9ZulSzKxjG87BblRky7hQ/viewform")
objIE.Visible = True
WScript.Sleep(3000)
IE.Document.getElementById("entry.1790931838").Value = "msdasdadm"
This is my code to auto-fill the form however I have not seemed to make it work. Also, I am not able to understand how to call the submit button and press that either.

This should get you started...
On Error Resume Next
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("https://docs.google.com/forms/d/e/1FAIpQLScOmcmtdsrm3RiX7FD9ur2eLPULL9ZulSzKxjG87BblRky7hQ/viewform")
objIE.Visible = True
WScript.Sleep(3000)
Dim input, inputs : Set inputs = objIE.document.getElementsByTagName("INPUT")
For Each input In inputs
If ("text" = input.getAttribute("type")) Then
input.value = "Just a test"
Exit For
End If
Next
Dim div, divs : Set divs = objIE.document.getElementsByTagName("DIV")
For Each div In divs
If ("button" = div.getAttribute("role") And "M2UYVd" = div.getAttribute("jsname")) Then
Call div.click()
Exit For
End If
Next
The Submit button is actually a parent DIV element with an associated JS onclick event handler defined. However, keep an eye on the jsname attribute, as the random name will most likely change between forms.
Hope this helps.

Related

SetTimeout() won't execute the function

So this is my code snippet:
'in VBScript
Sub Main()
Dim timeoutTimer
'more scripts here
'more scripts here
'more scripts here
timeoutTimer = window.setTimeout("alert()", 2000)
Call WaitForAnEvent() 'This function waits for an event to happen
'if there is no event then code execution stop's
'and wait
'more scripts here
'more scripts here
'more scripts here
End Sub
Sub alert()
MsgBox "Help!"
End Sub
What happens is, there are times when alert() is not triggered, and I don't have any idea why. I conducted some research about setTimeout() and they said that setTimeout will be triggered if the timer expires and as soon as there is an available opportunity to execute it. I believe after WaitForAnEvent() is invoked there will be an available opportunity for setTimeout to be executed but
sometimes it is and sometimes it is not.
Update -------------------------------------------------------------------------------
I had been reading a lot of articles about setTimeout and they all say(in short) that it cannot be triggered if the browser is busy doing something.
Now:
Is it correct to assume that the browser is doing something(infinite), and
setTimeout cannot find an available moment to trigger the function?
Is there a way in VBScript/Javascript to check if IE(browser) is currently doing somthing like rendering text or executing some scripts?
I think you should change your function name from alert to something that does not collide with elements exposed by the browser (there is a window.alert() function). Maybe this will work as is (not tested), but it is better to avoid confusion
The proper syntax to bind the event to the handler is to retrieve a reference to the function (here renamed)
window.setTimeout(GetRef("showAlert"), 2000)
Probably because I don't have enough information, but I don't see the need for your WaitForAnEvent() function. Events happen. You bind the function to execute on event and leave to the browser the work to call the event handler when needed
edited Just for a sample (adapted from a previous answer)
In this HTA, there are five events being handled: Start button press, Stop button press, Exit button press, Clock interval and File existence check
The basic idea is NOT to have code running all the time. The browser has the control and when an event happens (button pressed or interval reached) the code to handle the event is called and ends.
<html>
<head>
<title>ClockwithAlerts</title>
<HTA:APPLICATION
ID="ClockHTA"
APPLICATIONNAME="ClockHTA"
MINIMIZEBUTTON="no"
MAXIMIZEBUTTON="no"
SINGLEINSTANCE="no"
SysMenu="no"
BORDER="thin"
/>
<SCRIPT LANGUAGE="VBScript">
Const TemporaryFolder = 2
Dim timerID, timerFile
Sub Window_onLoad
window.resizeTo 600,280
SetClockTimer True
timerFile = window.setInterval(GetRef("CheckFilePresence"), 1500)
End Sub
Sub CheckFilePresence
Dim myFile
With CreateObject("Scripting.FileSystemObject")
myFile = .BuildPath(.GetSpecialFolder( TemporaryFolder ), "test.txt")
If .FileExists(myFile) Then
fileStatus.innerText = "FILE ["& myFile &"] FOUND"
Else
fileStatus.innerText = "File ["& myFile &"] is not present"
End If
End With
End Sub
Sub SetClockTimer( Enabled )
If Enabled Then
timerID = window.setInterval(GetRef("RefreshTime"), 1000)
RefreshTime
Else
window.clearInterval(timerID)
timerID = Empty
End If
StartButton.disabled = Enabled
StopButton.disabled = Not Enabled
End Sub
Sub RefreshTime
CurrentTime.InnerHTML = FormatDateTime(Now, vbLongTime)
End Sub
Sub ExitProgram
If Not IsEmpty(timerID) Then window.clearInterval(timerID)
If Not IsEmpty(timerFile) Then window.clearInterval(timerFile)
window.close()
End Sub
</SCRIPT>
</head>
<body>
<input id="checkButton" type="button" value="EXIT" name="run_button" onClick="ExitProgram" align="right">
<br><br>
<span id="CurrentTime"></span>
<br><br>
<input id="Stopbutton" type="button" value="Stop" name="StopButton" onclick="SetClockTimer(False)">
<input id="StartButton" type="button" value="Start" name="StartButton" onclick="SetClockTimer(True)">
<hr>
<span id="fileStatus"></span>
</body>
</html>
try remove quotes around your function name:
timeoutTimer = window.setTimeout(alert, 2000)

How to click a hyperlink embedded in an anchor tag of a html using vbscript?

I am able to open a browser and the page I want to work with. But the actual point of interest is the link called "Next". I want to click on that link but I tried unsuccessfully to get that element in my code. Here is what I have done so far.
Dim URL
Dim IE
Set IE = CreateObject("internetexplorer.application")
URL = "http://mylink.com/"
IE.Visible = True
IE.Navigate URL
Dim a
Set a = IE.Document.GetElementsByTagName("arrowRight")
For i = 0 To a.Length - 1
MsgBox "Found it"
a.Click
Exit For
Next
This is how the "Next" hyperlink is embedded in the page code:
<a class="arrowRight" href="http://SomeURL.com/150.html">Next</a>
arrowRight is a (CSS) class name, not a tag name, so you need to check for a tags with a class arrowRight:
For Each a In ie.document.getElementsByTagName("a")
If a.getAttribute("class") = "arrowRight" And a.innerText = "Next" Then
a.Click
Exit For
End If
Next

Calling multiple functions from single onClick in VBScript

I have created a VBScript function that allows users to install a network printer by clicking a button. This works great, but there is no feedback after clicking the button. I'm trying to add an alert after the button is clicked, but I'm having trouble with the syntax.
Here is the functioning onClick function for installing a printer:
<script type="text/vbscript">
function AddP(pName)
Set WshNetwork = CreateObject("Wscript.Network")
WshNetwork.AddWindowsPrinterConnection pName
end function
</script>
<td><input name="Button1" type="button" value="Add"></td>
If you want to give the user some indication something is happening while the printer is being added you could change the button state.
<script type="text/vbscript">
function AddP(pName)
Dim allButton1s
Set allButton1s = document.getElementsByName("Button1")
allButton1s(0).value = "Please wait..."
Set WshNetwork = CreateObject("Wscript.Network")
WshNetwork.AddWindowsPrinterConnection pName
MsgBox "Printer Added"
allButton1s(0).value = "Add"
end function
</script>
Simplest solution: Add a colon to your declaration which acts as a statement separator. This will allow you to call an additional function.
<a href="#" language="vbscript" onclick="AddP('\\PrinterName': msgbox 'Printer added')">
Just add this line after your WshNetwork.AddWindowsPrinterConnection line
MsgBox "Printer Added"

vb6: click button on HTMLDocument by code and wait for page to be loaded

i'm using the mshtml.tlb for loading/parsing html and i'd like extend it for clicking elements by code. the problem is trapping the loading-process after eg. a button was clicked.
in my specific case i'd like to perform a user-login.
here's my code:
Dim WithEvents m_doc As HTMLDocument
' load page
Set m_docNU = New HTMLDocument
Set m_doc = m_docNU.createDocumentFromUrl(m_url, vbNullString)
Do While m_doc.readyState = "loading" Or m_doc.readyState = "interactive"
DoEvents
Loop
set txtUsername = m_doc.getElementById("username")
set txtPasswort = m_doc.getElementById("passwort")
set myButton = m_doc.getElementById("submit")
myButton.click
now here's the big question mark: how to continue vb6- like "wait until page is loaded"?
i've tried as above using a do while-loop and checking the readyState, but for some reason the readyState doesn't change after clicking the button ..
any ideas?
thanks
ps: is there a more elegant way instead of the do while-loop? eg. using a progressbar?
use vb.net
wBrowser is a webbroser object
While wBrowser.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
End While

Clicking on a link that contains a certain string in VBS

I'm trying to run an automated vbs script that clicks on a link on a page. I have things of the form:
Const READYSTATE_COMPLETE = 4
Set IE = CreateObject("INTERNETEXPLORER.APPLICATION")
IE.Visible = true
IE.navigate ("http://mywebpage.com")
How do I then make it click on a link on that page that doesn't have an ID but is like
ClickMe!
Thanks!
Along the lines of
Dim LinkHref
Dim a
LinkHref = "link"
For Each a In IE.Document.GetElementsByTagName("A")
If LCase(a.GetAttribute("href")) = LCase(LinkHref) Then
a.Click
Exit For ''# to stop after the first hit
End If
Next
Instead of LCase(…) = LCase(…) you could also use StrComp(…, …, vbTextCompare) (see StrComp() on the MSDN).

Resources