views:

103

answers:

2

I have been trying to use this code to read the element by class in html/ajax knowing GetElementByClass is not a option in webBrowser.Document. I can't seem to get a return value then invoke the member. Is there a work around for this?

References: Getting HTMLElements by Class Name

Example:

<span class="example">(<a href="http://www.test.com/folder/remote/api?=test" onclick=" return do_ajax('popup_fodder', 'remote/api?=test', 1, 1, 0, 0); return false; " class="example">test</a>)</span>

Example code:

   HtmlElementCollection theElementCollection = default(HtmlElementCollection);
   theElementCollection = webBrowser1.Document.GetElementsByTagName("span");
   foreach (HtmlElement curElement in theElementCollection)
   {
        //If curElement.GetAttribute("class").ToString = "example"  It doesn't work.  
        // This should be the work around.
        if (curElement.OuterHtml.Contains("example"))
        {
            MessageBox.Show(curElement.GetAttribute("InnerText")); // Doesn't even fire.
            // InvokeMember(test) after class is found.
        }
    }
A: 

Why don't you use qjuery's selector engine for this. And also, where are you expecting the messagebox.show to appear ?

Vladimir Georgiev
Awesome, a fork of jquery? ;)
box9
Messagebox.Show was to show me the text that it retrieved. JQuery I am not familiar with. Got a example in this case?
Nightforce2
+2  A: 

this is a example of how i used the webbrowser control to find class specific elements and invoke Click on a link inside.

simplified >

   foreach (HtmlElement item in webBrowser1.Document.GetElementsByTagName("li"))
        {
            // use contains() if the class attribute is 
            // class="page_item page-item-218 current_page_item"
            //this to be more on spot! >> if (item.OuterHtml.Contains("class=\"page_item"))
            // or
            if (item.OuterHtml.Contains("page_item"))
            {
                foreach (HtmlElement childItem in item.Children)
                {
                    if (childItem.TagName == "A")
                    {
                        //Click the link/Current element
                        childItem.InvokeMember("Click");
                        break;
                    }
                }
                break;
            }
        } 

does this way work ?..

it works for me right here.

or maybe i misunderstand your question?

Chris Schroeder
This does not fire the event in the Example link. I am trying to fire the ajax event "Test" if you take a look at the link again . =)
Nightforce2