Generic functions which can be used in vbscript or vba macro to open URL, enter values in text box, click a link and get all links on a webpage.
The below code shows a sample of the usage.
It asks for username and password, log into the web application and clicks on all links one by one.
You can use the generic functions/subs available at the lower part of the code
Dim IE
Dim IE2
uname=InputBox("Enter your uname")
passs=InputBox("Enter your password")
Set IE = WScript.CreateObject("InternetExplorer.Application")
'set the ie properties
IE.ToolBar = 0
IE.StatusBar = 1
IE.Width = 999
IE.Height = 999
IE.Left = 0
IE.Top = 0
IE.Visible = 1
'navigate to a web page
IE.Navigate("http://www.yourURL/")
WaitForLoad IE
WebFormFill "
WebFormFill "
WaitForLoad IE
ClickLink ""
WaitForLoad IE
Set ElementCol = IE.Document.getElementsByTagName("a")
'Get all links on web page
'Grap/click the link we are looking for...
For Each Link In ElementCol
If instr(1,Link.innerHTML,". http://")>1 Then
mylink = Link.innerHTML
ClickLink mylink
End If
Next
Msgbox "Completed"
Sub GetOtherIE(URL)
Dim objInstances, objIE
Set objInstances = CreateObject("Shell.Application").windows
If objInstances.Count > 0 Then '/// make sure we have instances open.
For Each objIE In objInstances
If InStr(objIE.LocationURL,URL) > 0 then
Set IE2 = objIE
End if
Next
End if
End Sub
'Attaches to an existing instance of IE with matching URL
Sub GetIE(URL)
Dim objInstances, objIE
Set objInstances = CreateObject("Shell.Application").windows
If objInstances.Count > 0 Then '/// make sure we have instances open.
For Each objIE In objInstances
If InStr(objIE.LocationURL,URL) > 0 then
Set IE = objIE
End if
Next
End if
End Sub
'fills a form field and optionally submits form
Sub WebFormFill(fieldname,fieldvalue,submit)
Dim FormNr
Dim ItemNr
Dim TheForm
if IE.Document.All.Tags("FORM").Length = 0 then
MsgBox("No form found in page")
else
for FormNr = 0 to IE.Document.Forms.Length - 1
Set TheForm = IE.Document.Forms(FormNr)
for ItemNr = 0 to TheForm.Elements.Length - 1
if TheForm.Elements(ItemNr).Name = fieldname then
TheForm.Elements(ItemNr).Value = fieldvalue
If submit=1 then
TheForm.submit
end if
exit for
end if
next
next
end if
End Sub
'Navigates IE to specified URL
Sub Navigate(URL)
IE.Navigate URL
do while IE.Busy
loop
End Sub
'clicks specified link
Sub ClickLink(linktext)
Dim anchors
Dim ItemNr
Set anchors = IE.document.getElementsbyTagname("a")
For ItemNr = 0 to anchors.length - 1
If anchors.Item(ItemNr).innertext = linktext Then
anchors.Item(ItemNr).click
End If
next
'do while IE.Busy
'loop
End Sub
'This function extracts text from a specific tag by name and index
'e.g. TABLE,0 (1st Table element) or P,1 (2nd Paragraph element)
'set all to 1 to extract all HTML, 0 for only inside text without HTML
Function ExtractTag(TagName,Num,all)
dim t
set t = IE.document.getElementsbyTagname(Tagname)
if all=1 then
ExtractTag = t.Item(Num).outerHTML
else
ExtractTag = t.Item(Num).innerText
end if
End Function
Sub WaitForLoad(obj)
Do While obj.Busy
loop
wscript.sleep(100)
End Sub
No comments:
Post a Comment