DB Mika
DB Mika

Reputation: 15

Open form if page in webBrowser contains string

I'm quite new to C# and I'm working on a form that logs into a website, navigates to a specific page and then it should check if that page contains the words "Registered Plus" (This all in a webBrowser) Now I got this all working, except for the last part. I have been thinking and searching for hours about how to make my application check if the current webpage contains "Registered Plus"... This is my code for the button so far:

    private void btnReboot_Click(object sender, EventArgs e)
    {
        webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("username")[0].SetAttribute("value", usernameBox.Text);
        webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("password")[0].SetAttribute("value", passwordBox.Text);
        webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("submit")[0].InvokeMember("click");
        webBrowser1.Navigate("http://website.com/login.php?action=login");

    }

Does anyone know how to make it check if this page: http://website.com/login.php?action=login contains "Registered Plus" ? Or maybe a tutorial about how to do something similar to this? Thanks alot in advance. Have been stuck on this part for quite a while now..

UPDATE:

Got a comment telling me about DocumentText.Contains, tried this:

    private void btnReboot_Click(object sender, EventArgs e)
    {
        webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("username")[0].SetAttribute("value", usernameBox.Text);
        webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("password")[0].SetAttribute("value", passwordBox.Text);
        webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("submit")[0].InvokeMember("click");
        webBrowser1.Navigate("http://darkbox.nl/usercp.php?action=usergroups");
        if (webBrowser1.DocumentText.Contains("Registered Plus"))
        {
            label3.Text = "You're plus";
        }
        else
        {
            label3.Text = "You're not plus";
        }
    }

However it still tells me "You're not plus"

Am I doing it right this way? Or..

Upvotes: 0

Views: 1676

Answers (1)

Peter Lange
Peter Lange

Reputation: 2896

I am not able to test the code right now, but the big issue is that the call to the webBrowser1.Navigate is executed asynchroniously. Just like when you request it in IE or Chrome, it takes anywhere from a second to a minute for the page to load (or give an error.) On the other hand, your C# code takes barely a millisecond to move from the Navigate request to the next line of code.

You need to fire off your code checking the Document once the Navigate() method returns an event indicating it is done.

    private bool shouldEvaluateReponse = false;

    private void btnReboot_Click(object sender, EventArgs e)
    {
            webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("username")[0].SetAttribute("value", usernameBox.Text);
            webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("password")[0].SetAttribute("value", passwordBox.Text);
            webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("submit")[0].InvokeMember("click");
            shouldEvaluateResponse = true;
            webBrowser1.Navigate("http://darkbox.nl/usercp.php?action=usergroups");
    }

    public void WebBrowser1_Navigated(object sender,  WebBrowserNavigatedEventArgs e)
    {
       //ignore this method if the flag isn't set.
       if (!shouldEvaluateResponse) return; 

       //reset the flag so this method doesn't keep executing
       shouldEvaluateResponse = false;

       if (webBrowser1.DocumentText.Contains("Registered Plus"))
       {
                label3.Text = "You're plus";
       }
       else
       {
                label3.Text = "You're not plus";
       }
    }

Upvotes: 1

Related Questions