none
Webbrowse and InvokeScript RRS feed

  • Question

  • I'm trying to use Webbrowser to automate a download on a page with anchors like this one:

    <A href="Javascript:downloadBatch('198697193','4372')">

    The first number is constant in the sense that it uniquely identifies an archived document. The 2nd number is also a constant, it is simply a process identifier (refers to the download-archived-document process).

     

    I can download it using element.InvokeMember("click") but I have reliability issues with doing so and therefore I want to determine my other options. (I'm a begginer at automation).

    I've tried to duplicate every example of InvokeScript and Navigate that I could find on the internet. Nothing seems to work. I usually get the following runtime error (or with some methods i just get silence).

    A Runtime Error has occurred.

    Do you wish to Debug?

    Line: 0

    Error: Object expected

    Does this have something to do with frames? I don't know anything about frames but this page seems to be using them.

    Although I've had success sending a click to this anchor, I've had trouble at some websites clicking anchors, so I would prefer to learn how to invoke this script more directly. Can anyone help?

    Thursday, May 3, 2012 12:32 PM

Answers

  • Thanks but I think this was a frame issue after all. The following code did NOT work.

    WebBrowser1.Document.InvokeScript("downloadBatch", New Object() {"198697193", "4372"})

    But the following code does the trick:

    For Each frame As HtmlWindow In recurseFramesToGetAllFrames(document.Window.Frames) Try frame.Document.InvokeScript("downloadBatch", New Object() {"198697193", "4372"}) Catch ex As Exception End Try Next Private Function recurseFramesToGetAllFrames(ByVal Frames As HtmlWindowCollection) As List(Of HtmlWindow) Dim theList As New List(Of HtmlWindow) For Each frame As HtmlWindow In Frames theList.Add(frame) If Not IsNothing(frame.Document.Window) AndAlso Not IsNothing(frame.Document.Window.Frames) Then theList.AddRange(recurseFramesToGetAllFrames(frame.Document.Window.Frames)) End If Next Return theList End Function



    • Edited by jal2 Thursday, May 3, 2012 1:09 PM
    • Marked as answer by jal2 Thursday, May 3, 2012 1:10 PM
    Thursday, May 3, 2012 1:08 PM

All replies

  • invokescript works fine if you want an perfect example of real life scenario usage check in my bin\debug folder in my kinect multipoint project here for html or htm file which has the vbscript: http://kinectmultipoint.codeplex.com. I use invoke script for dsf(device simulation framework) device emulation and you have to have a sub in vbscript to pass to invokescript for it to work properly and if you do the error is in the vbscript somewhere.

    P.S. you need to allow blocked content if that message appears for your code to execute properly too because some parts of code will not run properly if the vbscript code content is not unblocked especially when calling com components in vbscript or external classes.


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - "Sherlock holmes" "speak softly and carry a big stick" - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://jefferycarlsonblog.blogspot.com/



    • Edited by The Thinker Thursday, May 3, 2012 12:47 PM
    • Proposed as answer by The Thinker Thursday, May 3, 2012 12:47 PM
    Thursday, May 3, 2012 12:46 PM
  • Thanks but I think this was a frame issue after all. The following code did NOT work.

    WebBrowser1.Document.InvokeScript("downloadBatch", New Object() {"198697193", "4372"})

    But the following code does the trick:

    For Each frame As HtmlWindow In recurseFramesToGetAllFrames(document.Window.Frames) Try frame.Document.InvokeScript("downloadBatch", New Object() {"198697193", "4372"}) Catch ex As Exception End Try Next Private Function recurseFramesToGetAllFrames(ByVal Frames As HtmlWindowCollection) As List(Of HtmlWindow) Dim theList As New List(Of HtmlWindow) For Each frame As HtmlWindow In Frames theList.Add(frame) If Not IsNothing(frame.Document.Window) AndAlso Not IsNothing(frame.Document.Window.Frames) Then theList.AddRange(recurseFramesToGetAllFrames(frame.Document.Window.Frames)) End If Next Return theList End Function



    • Edited by jal2 Thursday, May 3, 2012 1:09 PM
    • Marked as answer by jal2 Thursday, May 3, 2012 1:10 PM
    Thursday, May 3, 2012 1:08 PM
  • Suspecting it was frames-issue, I started writing this code last night but didn't finish it till a few minutes ago - I really didn't expect it to work but lo and behold....

    Thursday, May 3, 2012 1:11 PM