4 Replies - 7099 Views - Last Post: 22 March 2010 - 01:11 PM Rate Topic: -----

#1 Mirge   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 18-March 10

C# Web Browser control question

Posted 22 March 2010 - 12:26 PM

Hi folks,

I've got a web browser control docked to the top of my form... and a label beneath it that is just used for a few debug/test statements (ie: "GOT HERE", or "URL=lfdsajlks")...

I know how to reach elements by using, for example, webBrowser1.document.GetElementById("...") and webBrowser1.document.GetElementsByTagName("...")..

But my question is, how do I test if an element exists or not? I tried using:

            HtmlElement emailField = webBrowser1.document.GetElementById("login_email");
            if (emailField.ToString().Length > 0)
            {
                label1.Text = "Found email field: " + emailField.Id.ToString();



which worked... but at the same time, doesn't... lol! It works as in it detects when the email field IS on the page, and acts accordingly... but when it's NOT on the page, nothing happens. Nothing in my else {...} block is executed.

Thanks :)

Is This A Good Question/Topic? 0
  • +

Replies To: C# Web Browser control question

#2 Mirge   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 18-March 10

Re: C# Web Browser control question

Posted 22 March 2010 - 12:31 PM

Ok, looked at the Output window after I ran the program, and noticed an exception.. so I inserted try { } catch { } blocks... made my code work.. but I guess I'm just wondering if there was a different way to see if an element existed or not?
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: C# Web Browser control question

Posted 22 March 2010 - 12:51 PM

How about checking for == null after the Get...() ? Does that work?
Was This Post Helpful? 1
  • +
  • -

#4 Mirge   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 18-March 10

Re: C# Web Browser control question

Posted 22 March 2010 - 12:55 PM

View PostJackOfAllTrades, on 22 March 2010 - 11:51 AM, said:

How about checking for == null after the Get...() ? Does that work?


Genius!

HtmlElement emailField = webBrowser1.document.GetElementById("login_email");
if(emailField != null)
{



the if() statement replaced my "try" portion... and worked like a dang charm :)

The actual program is just for my own private use... uses a web browser control to navigate to paypal, automatically logs in, and once it reaches the "Home" page, it shows a popup with my Paypal balance... used 2 regular expressions to extract the actual numeric balance.

It doesn't serve any actual REAL purpose, it's just for my own experimentation since I'm still very unfamiliar with what C#/.NET is capable of, and I like experimenting to learn :) I find I am able to retain what I learn longer if I actually take a hands-on approach.

Anyhoo, thanks much! Lesson learned :)
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: C# Web Browser control question

Posted 22 March 2010 - 01:11 PM

Cool! It's how you would do it in Javascript, so I thought the same might work in C#. Although I have done significant work with the browser control, it has been in C/C++ and not really content-related.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1