Sam Clark-Ash
Sam Clark-Ash

Reputation: 175

Press button on webbrowser

I have a youtube page loaded (http://www.youtube.com/user/SeaNanners/videos) and I would like to press the 'Load More' button, I have tired several things such as;

foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("input"))
        {
            if (el.GetAttribute("class").Equals("yt-uix-load-more load-more-button yt-uix-button yt-uix-button-default yt-uix-button-size-default"))
            {
                    el.InvokeMember("click");
            }
        }

As well as attempting other answers given on similar questions; C# How to Click Button auttomaticly via WebBrowser Button click in web browser

None seem to actually push the button.

Upvotes: 0

Views: 367

Answers (1)

makim
makim

Reputation: 3284

Got it working, quick and dirty maybe you can write it a little bit more graceful ;-)

UPDATE:

I´ve read your Question that you asked before, and updated the code accordingly. With this piece of code all Videos of a channel gets loaded (this may take a while with the webbrowserControl)

private void button1_Click(object sender, EventArgs e)
{
    bool found = true;
    while (found)
    {
        if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
        {
            found = ClickLoadMoreButton();
            Application.DoEvents();
        }
    }
}

private bool ClickLoadMoreButton()
{
    var buttons = webBrowser1.Document.GetElementsByTagName("button");
    foreach (HtmlElement el in buttons)
    {
        var firstChild = el.FirstChild;
        if (firstChild != null)
        {
            if (!string.IsNullOrEmpty(firstChild.InnerText))
            {
                if (firstChild.InnerText.ToLower().Replace(" ", string.Empty).Equals("loadmore"))
                {
                    el.InvokeMember("click");
                    return true;
                }
            }
        }
    }
    return false;
}

Upvotes: 1

Related Questions