Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select a class by GetElementByClass and click on it programmatically

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.
        }
    }
like image 638
Nightforce2 Avatar asked Aug 08 '10 21:08

Nightforce2


People also ask

How do you select an element with class name text?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class. To do this, start with the element name, then write the period (.)

What does getElementsByClassName method return?

The getElementsByClassName method of Document interface returns an array-like object of all child elements which have all of the given class name(s). When called on the document object, the complete document is searched, including the root node.

Is getElementsByClassName ordered?

getElementsByClassName("a") will reliably list them in order: d1, d2, d3, d4, d5. Note that getElementsByClassName is not supported by some older browsers, notably IE8.


2 Answers

I admit it's not very intuitive but you need to use GetAttribute("className") instead of GetAttribute("class")

HtmlElementCollection theElementCollection = default(HtmlElementCollection);
theElementCollection = webBrowser1.Document.GetElementsByTagName("span");
foreach (HtmlElement curElement in theElementCollection)
{
    if (curElement.GetAttribute("className").ToString() == "example")
    {
        MessageBox.Show(curElement.GetAttribute("InnerText")); // Do something you want
    }
}
like image 61
Joe Terhune Avatar answered Sep 26 '22 17:09

Joe Terhune


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?

like image 37
Kristoffer Schroeder Avatar answered Sep 23 '22 17:09

Kristoffer Schroeder