Automate IE without a tool - VBscript




Here is a basic plain  vbscript which I used to automate a daily chore activity.

Option Explicit

strUser = "Ady"
strPwd = "pwd"

Dim objLink , ie , WshShell , colLinks
Dim i , intDay , intOff , intcount , btn, strText, j
Dim strUser , strPwd , hwnd, Wnd, oShell, objImage, objImages , strOut



Sub WaitForLoad(obj)
   Do While ie.Busy
   Loop
  
   Do While obj.readyState <> 4
   Loop
   wscript.sleep(100)
End Sub

'Create ie and shell object
Set ie = WScript.CreateObject("InternetExplorer.Application")
Set WshShell = WScript.CreateObject("WScript.Shell")

'set the ie properties
ie.ToolBar = 1
ie.StatusBar = 1
ie.Width = 1500
ie.Height = 999
ie.Left = 0
ie.Top = 0
ie.Visible = 1

With ie
hwnd = .hwnd
      .Navigate "http://test/default.aspx"
End With

Set oShell = CreateObject("Shell.Application")
For Each Wnd In oShell.Windows
        If hwnd = Wnd.hwnd Then Set ie = Wnd
Next

'wait until the page has loaded before continuing
WaitForLoad(ie)

'Enter the user details
Enter the user details


ie.Document.getElementByID("txtUser").Value = strUser
ie.Document.getElementByID("pwdPass").Value = strPwdw
WaitForLoad(ie)

'Alert Box
WshShell.AppActivate "Main - Windows Internet Explorer"


For i=1 to 10
   wshShell.sendkeys "{TAB}"
   wscript.sleep(200)
Next

ie.Document.getElementsByName("ctl00$ContentPlaceHolder1$btnView").Item(0).Click
wscript.sleep(5000)
ie.Document.All.Item("chkall").click


For Each objLink In colLinks

   ' check text for link
    If InStr(UCase(objLink.innerHTML), "OFF") > 0 Then
   ' navigate to the first match and exit the for...next loop
    intcount = intcount + 1
    If intcount = intOff then
            'ie.Navigate objLink.href
            objLink.click
            Exit For
     End If
     End If
Next
wscript.sleep(2000)msgbox "done"
Set ie = nothing
Set colLinks = nothing
Set WshShell = nothing





Comments

  1. Hi. I am getting an error for the the line:
    If hwnd = Wnd.hwnd Then Set ie = Wnd

    "Automation type not supported in VBScript"

    What could be the cause?

    ReplyDelete

Post a Comment

Popular posts from this blog

Software Testing @ Microsoft

Trim / Remove spaces in Xpath?