DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: [Help] - Check if a webpage contains some text then do something (C#)  (Read 15,060 times)

KynloStephen66515

  • Animated Giffer in Chief
  • Honorary Member
  • Joined in 2010
  • **
  • Posts: 3,772
    • Participated in NANY Challenge Three Times
    • Cody Award
    • Coding Snacks
    • Official Forum Clown
    • Volunteers/Staff
    • Writer
    • Hardware Hacker
    • DJ Badge - DJ
    • Linux User
Actually going to pull my hair out if I can't work this out lol

Trying to work out how the hell to check if a webpage contains a certain string of text, and if it does, then do something (I know how to get it to do something afterwards...just can't work out how to CHECK)

In non-code, this would be:

if webPage.ContainsString("No Messages Found") {  //Do Something } else { //Do Something Different }

Bit vague on this I know, I suck at explaining these things :(

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,649
    • Participated in NANY Challenge Eleven Times
    • Coding Snacks
    • Linux User
    • Hardware Hacker
Re: [Help] - Check if a webpage contains some text then do something (C#)
« Reply #1 on: September 07, 2014, 08:33 PM »
You can use a webbrowser control to fetch the page, then check its content with a regex.
/Ton
Ath's software collection, Quote: "A problem well stated is a problem half-solved." (Charles Kettering)

KynloStephen66515

  • Animated Giffer in Chief
  • Honorary Member
  • Joined in 2010
  • **
  • Posts: 3,772
    • Participated in NANY Challenge Three Times
    • Cody Award
    • Coding Snacks
    • Official Forum Clown
    • Volunteers/Staff
    • Writer
    • Hardware Hacker
    • DJ Badge - DJ
    • Linux User
Re: [Help] - Check if a webpage contains some text then do something (C#)
« Reply #2 on: September 07, 2014, 08:39 PM »
You can use a webbrowser control to fetch the page, then check its content with a regex.

I was already using a webBrowser control and was looking into using the HTML Agility Pack I got off NuGet to do this, but it is seeming like a pain in the ass...Also..I suck BADLY with Regex lol

Renegade

  • Tell me something you don't know...
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
    • Participated in NANY Challenge Three Times
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: [Help] - Check if a webpage contains some text then do something (C#)
« Reply #3 on: September 08, 2014, 05:52 AM »
I was already using a webBrowser control and was looking into using the HTML Agility Pack I got off NuGet to do this, but it is seeming like a pain in the ass...Also..I suck BADLY with Regex lol

You need to decide on which control you will use. I'll use the stock control here.

Code: (csharp)
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webBrowser1.DocumentText.Contains("No Messages Found")) {
                this.Text = "No Messsaged Found found";
            }
        }

That is all you need.
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,649
    • Participated in NANY Challenge Eleven Times
    • Coding Snacks
    • Linux User
    • Hardware Hacker
Re: [Help] - Check if a webpage contains some text then do something (C#)
« Reply #4 on: September 08, 2014, 06:48 AM »
Also..I suck BADLY with Regex lol
There's a fix for that, reported here, in our DC forum :up:
/Ton
Ath's software collection, Quote: "A problem well stated is a problem half-solved." (Charles Kettering)

KynloStephen66515

  • Animated Giffer in Chief
  • Honorary Member
  • Joined in 2010
  • **
  • Posts: 3,772
    • Participated in NANY Challenge Three Times
    • Cody Award
    • Coding Snacks
    • Official Forum Clown
    • Volunteers/Staff
    • Writer
    • Hardware Hacker
    • DJ Badge - DJ
    • Linux User
Re: [Help] - Check if a webpage contains some text then do something (C#)
« Reply #5 on: September 08, 2014, 10:01 PM »
I was already using a webBrowser control and was looking into using the HTML Agility Pack I got off NuGet to do this, but it is seeming like a pain in the ass...Also..I suck BADLY with Regex lol

You need to decide on which control you will use. I'll use the stock control here.

Code: (csharp)
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webBrowser1.DocumentText.Contains("No Messages Found")) {
                this.Text = "No Messsaged Found found";
            }
        }

That is all you need.

I couldn't actually get that to even DO anything lol (Although, it does compile...so might be something I did(n't) do.)

hamradio

  • Amateur Radio Guy
  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 893
    • Plugin Coding Medal
    • Participated in NANY Challenge Fifteen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
    • Hardware Hacker
Re: [Help] - Check if a webpage contains some text then do something (C#)
« Reply #6 on: September 09, 2014, 03:52 AM »
One way to not even use the web browser control should be as simple as this...

Code: (csharp)
WebClient myWebClient = new WebClient();
string siteSource = myWebClient.DownloadString("PLEASECHANGETOYOURURL");

if (siteSource.Contains("YOURTEXT"))
{
// Your string found code here.
}
else
{
// Your string not found code here.
}

For more info: (Note: I'm not affiliated with Dot Net Perls in any size, shape, and/or form.)

http://www.dotnetperls.com/webclient
« Last Edit: September 09, 2014, 04:01 AM by hamradio »

Renegade

  • Tell me something you don't know...
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
    • Participated in NANY Challenge Three Times
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: [Help] - Check if a webpage contains some text then do something (C#)
« Reply #7 on: September 09, 2014, 12:29 PM »
Here's the project:

[attachurl=#]

You can run the debug or compile it yourself. It's done in VS2010.

The WebClient method is actually better, but the way I have that there is a bit more visual with a control, etc. Also, you can actually surf with it.
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

Original address: https://www.donationcoder.com/forum/index.php?topic=38902.0