1

I'm having this wierd problem within the application I'm currently working on.

string searchText = "onMouseOver=\"CallList_onMouseOver(this);\" id=\"";
List<int> searchOrders = AllIndexesOf(scraper.clientBrowser.DocumentText, searchText);
StringBuilder sb = new StringBuilder();

for (int i = 0; i < searchOrders.Count; i++)
{
    string order = scraper.clientBrowser.DocumentText.Substring(searchOrders[i] + searchText.Length, 6);
    scraper.clientBrowser.Document.GetElementById(order).InvokeMember("Click");

    for (int j = 0; j < scraper.clientBrowser.Document.Window.Frames.Count; j++)
    {
        if (scraper.clientBrowser.Document.Window.Frames[j].Document != null && scraper.clientBrowser.Document.Window.Frames[j].Document.Body != null)
        {
            string orderText = scraper.clientBrowser.Document.Window.Frames[j].Document.Body.InnerText ?? "Nope";
            //MessageBox.Show(j + Environment.NewLine + orderText);
            if (!orderText.Contains("Nope"))
            {
                sb.AppendLine(orderText + Environment.NewLine);
            }
        }
    }
}
Clipboard.SetText(sb.ToString());

The thing is, whenever I uncomment the MessageBox.Show, I can clearly see orderText is filled with another value than "Nope", the Stringbuilder gets filled, and the correct text is copied.

However if I comment the Messagebox.Show, the outcome of this loop is always "Nope". I'm stuck here, I have no idea what could cause something like this.

The scraper.clientBrowser is a System.Windows.Forms.WebBrowser.

Update: Solved the issue by waiting for the document to be loaded, created this mechanism:

public bool DocumentLoaded
{
    get { return documentLoaded; }
    set { documentLoaded = value; }
}

private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    this.DocumentLoaded = true;
    this.clientBrowser = sender as WebBrowser;
}

void clientBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    this.DocumentLoaded = false;
}

Then in the class I'm using:

while(!scraper.DocumentLoaded)
{
    System.Threading.Thread.Sleep(100);
}
4
  • 1
    problem might be, that your code gets body.InnerText BEFORE the page is loaded after invoking "Click". When you have messageBox uncommented, the browser has some more time to load the page and get correct data Commented Feb 22, 2016 at 12:08
  • Did you wait for the document to load? Try doing all this on the DocumentCompleted event (might need some additional fiddling if you are using frames, which you seem to be) Commented Feb 22, 2016 at 12:08
  • DocumentCompleted is not firing after clicking the element @Jcl Commented Feb 22, 2016 at 12:26
  • System.Threading.Thread.Sleep(2000); after each click is also not working Commented Feb 22, 2016 at 12:29

2 Answers 2

2

It sounds like you need to ensure that the page is fully loaded, like there might be a race condition. I would suggest wiring up the WebBrowser.DocumentCompleted event, and then attempting your scrapping logic.

Update

I overlooked this initially, this certainly has something to do with your issue. The line where you are invoking a click, like so scraper.clientBrowser.Document.GetElementById(order).InvokeMember("Click");. This is done in the iteration, which will more than likely manipulate the DOM -- will it not? I suggest going about this problem entirely different. What are you trying to achieve exactly, (not how you're trying to do it)?

With this alone, I would suggest that you refer to this SO Q/A and look at how they're waiting for the click to finish.

Sign up to request clarification or add additional context in comments.

4 Comments

Will try this, hold on a few moments.
LoadCompleted works for the WPF version (on System.Windows.Controls). The correct one for Winforms is DocumentCompleted. You may want to check if the URL on the parameters for DocumentCompleted is the same as the URL you are navigating to, specially if you are using frames (DocumentCompleted will fire up several times)
DocumentCompleted is not firing after clicking the element. @Jcl
System.Threading.Thread.Sleep(2000); after each click is also not working
1

Only one thing I can guest here:
When you uncomment MessageBox.Show, at the time the message box show the info, the clientBrowser use this time to finish loading page. Then when you press OK on message box, the page is load completed, so you get the result. When you comment it, you dont wai for page loaded, so the result is diffent.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.