Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

webbrowser control - check if all frames were loaded (alternative to “DocumentCompleted”)

$
0
0
Hello everyone!

I am writing a program that navigates through a website containing two frames. I am using WinForms webBrowser control. (I tried using Selenium, but I need the browser to be either visible or hidden according to users' need and Selenium cannot hide the browser control)

I had the same program written in visual basic script, where I have used the following code as a subprocedure to make sure that everything is loaded before trying to invoke click events etc.

Sub CheckIfLoaded
    Do while appIE.Busy = True OR appIE.ReadyState <> 4 : Wscript.Sleep(100) : Loop
End Sub

I am looking for something similar in C#. I don't think that WebBrowswer.DocumentCompleted event is the solution here, because as far as I understand the usage of it, I would have to put whole code into one webBrowser1_DocumentCompleted method in a large number of if statements, which seems really wrong.

The pseudo-code that shows my intentions would be:

private void DoOneSpecificThing (string[] ListOfThings)
{
    foreach (string ID in ListOfThings)
    {
      open webbrowser and navigate to page;
       CheckIfLoaded();
      Log In and load the first page;
       CheckIfLoaded();
      Tick a bunch of tickboxes, write in textboxes etc;
      Submit;
       CheckIfLoaded();
      Go to the next page;
       CheckIfLoaded();
      And do more stuff;
      Submit;
    }
}

There are a lot of procedures like that in my code (e.g. for logging in, for navigating to selected page, for filling forms, for setting options, for generating reports), so I would like to have one simple thing that would make sure that the page is loaded before trying to access its element, as I had in vbs.

I tried the following two solutions: (in the first one, the application never gets to load a page)

        int counter = 0;
        while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
        {
            counter += 1;
        }


(in the second, the application doesn't actually wait for the page to load, so I tend to get null reference errors and this kind of stuff InvalidArgument=Value of 'nav' is not valid for 'windowId'.)

        while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
        {
            Application.DoEvents();
        }

Help will be greatly appreciated! I have a feeling that Application.DoEvents() is a wrong way to go, but I have no idea what to do and whole Internet says "just put your code in "DocumentCompleted" event.
Thanks in advance!

Bartosz

Viewing all articles
Browse latest Browse all 31927

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>