topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Tuesday December 3, 2024, 11:53 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

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

KynloStephen66515

  • Animated Giffer in Chief
  • Honorary Member
  • Joined in 2010
  • **
  • Posts: 3,758
    • View Profile
    • Donate to Member
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,629
    • View Profile
    • Donate to Member
Re: [Help] - Check if a webpage contains some text then do something (C#)
« Reply #1 on: September 07, 2014, 03:33 PM »
You can use a webbrowser control to fetch the page, then check its content with a regex.

KynloStephen66515

  • Animated Giffer in Chief
  • Honorary Member
  • Joined in 2010
  • **
  • Posts: 3,758
    • View Profile
    • Donate to Member
Re: [Help] - Check if a webpage contains some text then do something (C#)
« Reply #2 on: September 07, 2014, 03: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

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: [Help] - Check if a webpage contains some text then do something (C#)
« Reply #3 on: September 08, 2014, 12: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: C# [Select]
  1. private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  2.         {
  3.             if (webBrowser1.DocumentText.Contains("No Messages Found")) {
  4.                 this.Text = "No Messsaged Found found";
  5.             }
  6.         }

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,629
    • View Profile
    • Donate to Member
Re: [Help] - Check if a webpage contains some text then do something (C#)
« Reply #4 on: September 08, 2014, 01:48 AM »
Also..I suck BADLY with Regex lol
There's a fix for that, reported here, in our DC forum :up:

KynloStephen66515

  • Animated Giffer in Chief
  • Honorary Member
  • Joined in 2010
  • **
  • Posts: 3,758
    • View Profile
    • Donate to Member
Re: [Help] - Check if a webpage contains some text then do something (C#)
« Reply #5 on: September 08, 2014, 05: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: C# [Select]
  1. private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  2.         {
  3.             if (webBrowser1.DocumentText.Contains("No Messages Found")) {
  4.                 this.Text = "No Messsaged Found found";
  5.             }
  6.         }

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

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 884
  • Amateur Radio Guy
    • View Profile
    • HamRadioUSA.net
    • Read more about this member.
    • Donate to Member
Re: [Help] - Check if a webpage contains some text then do something (C#)
« Reply #6 on: September 08, 2014, 10:52 PM »
One way to not even use the web browser control should be as simple as this...

Code: C# [Select]
  1. WebClient myWebClient = new WebClient();
  2. string siteSource = myWebClient.DownloadString("PLEASECHANGETOYOURURL");
  3.  
  4. if (siteSource.Contains("YOURTEXT"))
  5. {
  6. // Your string found code here.
  7. }
  8. else
  9. {
  10. // Your string not found code here.
  11. }

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 08, 2014, 11:01 PM by hamradio, Reason: Added for more info... »

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: [Help] - Check if a webpage contains some text then do something (C#)
« Reply #7 on: September 09, 2014, 07:29 AM »
Here's the project:

* WebCheckStephen.zip (30.35 kB - downloaded 573 times.)

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