Jump to content
Sign in to follow this  

set URLs from document into array - VBscript

Recommended Posts


Hi all.

I recently aquired an internship at the Department of Technology, in Timonium, MD, working for the BCPS school system. One of my jobs is to create scripts with a software called TestComplete. This creates a user-simulation that can be used to test if a system still works correctly after an update.

For one of the scripts I need to find out if a link to download a .mw2 file is present on a page, and navigate to it. I assume the best way to do this would be to set the URLs on the page to an array, and check them against the string ".mw2", and when a comparison is found, go to that page.

How do I do this?

Here is my VBscript so far: (keep in mind I have never written VBscript before yesterday, any improvements are welcome)

Sub Main
  ' Enter your code here. 
End Sub

Sub Test1
  Dim page
  Dim all
  Dim textbox
  dim bodyText
  dim searchResult
  dim downloadResult
  dim linkResult
  searchTerm = "compass"     'this will be queried using the CBTIA page's search engine
  searchString = "Compass Rose"     'this will be searched for after the searchTerm is queried
  Set page = Sys.Process("iexplore").Page("http://www.bcps.org/")
  Call page.document.all.Offices.Click(37, 8)
  'Please wait until download completes: "http://www.bcps.org/offices/"
  'Please wait until download completes: "http://www.bcps.org/offices/oit/"
  'Please wait until download completes: "http://www.bcps.org/apps/CBTIA/"
  Set all = page.document.all
  Call all.ddlGradeLevel.ClickItem("3")
  Set textbox = all.txtKeyword
  Call textbox.Click(25, 10)
  Call textbox.Keys(searchTerm)
  'Please wait until download completes: "http://www.bcps.org/apps/CBTIA/default.aspx"
  searchResult = SearchPage(searchString, page)'sets to TRUE if searchString was found
  Set iexplore = Sys.Process("iexplore")
  'Please wait until download completes: "http://www.bcps.org/apps/CBTIA/cbtia.aspx?id=4154"
  linkResult = FindLink(page)
  If linkResult = True Then
    downloadResult = DownloadCheck(iexplore)
  End If
End Sub

Function SearchPage(searchString, page)

  bodyText = page.document.all.Item("body").innerHTML
  If InStr(bodyText, searchString) > 0 Then
      SearchPage = TRUE
    Log.Warning(searchString + " was not found.")
    SearchPage = FALSE
  End If

End Function

Function DownloadCheck(iexplore)

  If iexplore.Window("#32770", "File Download").Exists < 0 Then 'did a download window appear?
    iexplore.Window("#32770", "File Download").Window("Button", "Cancel").ClickButton
    Log.Warning("File was not found! No .mw2 present")
  End If

End Function

Function FindLink(page)

  set urls = page.document.all.tags("a")  'THIS IS THE LINE THAT SHOULD CREATE THE ARRAY BUT IT DOESN'T WORK
  For x = 0 to (urls.length)-1
    If InStr(urls(x).innerHTML, ".mw2") > 0 Then
    FindLink = TRUE
      'page.document.all.Item(263).Click 'click on .mw2 link
      Set edit = Sys.Process("iexplore").IEFrame(0).Window("Edit", "", 1)
      edit.wText = urls(x).innerHTML
      Call edit.Keys("[Enter]") 
      Exit Function
    End If
  FindLink = FALSE
  Log.Warning("Link to .mw2 Not Found!")

End Function

The code that navigates was automatically generated, I wrote the other functions, loops, etc.

Please help!

Share this post

Link to post
Share on other sites

got it.

Sub Main

  Call GetSchoolLinksFrom_BCPS

End Sub


Sub GetSchoolLinksFrom_BCPS

  'Initialize everything - this assumes a maximum of 4000 links on a page - increase it if you need to have more.

  'Also, remember that this is a zero based array.

  Dim iexplore, page, linkCount, debugmode

  Dim linkArray(4000)

  debugmode = True

  linkCount = 0


  'Now that everything is initialized, open the browser (assumes all browser windows are closed)


  Set iexplore = Sys.Process("iexplore")

  Set page = iexplore.Page("*")


  'Now go the specified webpage - note the ToURL function will automatically wait before continuing

  Call page.ToURL("http://www.bcps.org/")


  'Click the schools link

  Call page.document.all.schools.Click



  'Call the super special procedure to find all the <a> tags. 

  'The debugmode will make things be logged or not as it finds each link. 

  Call FindAllLinks(page.document.all, linkCount, linkArray, debugmode)


  if debugmode Then

    if linkCount>0 Then

      Log.Message("Total links found:" & linkCount)

      Log.Message("First link found:" & linkArray(0))            'The first link is ALWAYS zero

      Log.Message("Final link found:" & linkArray(linkCount-1))  'Remember this is a zero based array so subtract 1 from linkCount


      Log.Message("No links were found at this webpage:" & page.URL)

    End if  


  End if


  'From here you can do whatever you need to withthe linkArray()


  'Finally close all the browsers


End Sub


Sub FindAllLinks(d, lc, l, dbg)

  'This routine should find all a href links in a page.  It assumes you are using the DOM model.

  'p = page to search thru

  'lc = Number of links found

  'l = array of the links found

  'dbg = flag to say if you want things logged as they are found for debugging

  Dim i

  if dbg then Log.Message("Started searching... Number of Child Objects:" & d.ChildCount) 

  lc = 0

  For i = 0 To (d.ChildCount - 1)

     if d.Child(i).tagname <> "A" Then

        if dbg then Log.Message("Child(" & i & ") is not <a> tag.  It has a tagname of '" & d.Child(i).tagname & "'.")       




        if dbg then Log.Message("Link #" & lc & " - Child(" & i & ") is a href='" & d.Child(i).href & "'.")            

     End if


End Sub

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this