How to invoke element in a WebBrowser by the class name(s)?

512 views Asked by At

I'm trying to make a simple Facebook client. One of the features should allow the user to post content on the homepage/his profile. It logs the user in (works fine, all of the elements have got ids on Facebook) and then inserts the data in the corresponding field (works fine as well), but then it needs to click the "Post" button. However, this button doesn't have any id. It only has got a class.

<li><button value="1" class="_42ft _4jy0 _11b _4jy3 _4jy1 selected _51sy" data-ft="&#123;&quot;tn&quot;:&quot;+&#123;&quot;&#125;" type="submit">Posten</button></li>

('Posten' is 'Post' on German.)

I've been looking around the internet for a few hours now and tried different solutions. My most current solution is to search the item by it's inner content ("Posten") and then invoke it. Doesn't work. It inserts the text but doesn't invoke the button. Here's the code:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (postHomepage)
            {
                webBrowser1.Document.GetElementById("u_0_z").SetAttribute("value", metroTextBox1.Text);
                GetButtonByInnerText("Posten").InvokeMember("click");
                postHomepage = false;
            }
        }

HtmlElement GetButtonByInnerText(string SearchString)
        {

            String data = webBrowser1.DocumentText;
            //Is the string contained in the website
            int indexOfText = data.IndexOf(SearchString);
            if (indexOfText < 0)
            {
                return null;
            }
            data = data.Remove(indexOfText); //Remove all text after the found text
                                             //These strings are a list of website elements
                                             //NOTE: These need to be updated with ALL elements from list such as:
                                             //      http://www.w3.org/TR/REC-html40/index/elements.html
            string[] strings = { "<button" };
            //Split the string with these elements.
            //subtract 2 because -1 for index -1 for elements being 1 bigger than wanted
            int index = (data.Split(strings, StringSplitOptions.None).Length - 2);
            HtmlElement item = webBrowser1.Document.All[index];
            //If the element is a div (which contains the search string
            //we actually need the next item.
            if (item.OuterHtml.Trim().ToLower().StartsWith("<li"))
                item = webBrowser1.Document.All[index + 1];
            //txtDebug.Text = index.ToString();

            return item;

        }

(This is a quick solution which I edited for my use, not very clean). What's wrong here?

1

There are 1 answers

0
Denis  Yarkovoy On

It does not look like your GetButtonByInnerText() method is searching for the button element correctly.

Here is simple replacement for you to try:

HtmlElement GetButtonByInnerText(string SearchString)
{
 foreach (HtmlElement el in webBrowser1.Document.All) 
   if (el.InnerText==SearchString)
    return el;
}