pythoner
pythoner

Reputation: 79

How to manipulate webpage by weBrowser control after JS is executed?

I have webpage which gets data by json and then generates html from that data. I want to be able to do element.invokeMember("click"); (webBrowser winForms control) on source generated by JS. How to do that in c#?

I can see the source in firebug only.

What have I already done: ( _ie from here: How to make WebBrowser wait till it loads fully?)

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        webBrowser1.ProgressChanged += new WebBrowserProgressChangedEventHandler(_ie);
    }
    private void _ie(object sender, WebBrowserProgressChangedEventArgs e)
    {
        int max = (int)Math.Max(e.MaximumProgress, e.CurrentProgress);
        int min = (int)Math.Min(e.MaximumProgress, e.CurrentProgress);
        if (min.Equals(max))
        {
            Console.Write("complete");
            var menus = webBrowser1.Document.GetElementsByTagName("menu");
            Console.Write(menus.Count);
            var votes = new List<HtmlElement>();
            foreach (HtmlElement menu in menus)
            {
                Console.Write("found");
                var ass = menu.GetElementsByTagName("a");
                foreach (HtmlElement a in ass)
                {
                    if (a.GetAttribute("class").Contains("vote-up"))
                    {
                        a.InvokeMember("click");
                    }
                }
            }
        }
    }
    private void button1_Click(object sender, EventArgs e)
    {
        webBrowser1.Navigate("xxxxx");
       
    }
    


}

HTML: http://pastebin.com/0KGCwtqs copied from firebug, so some tags are collapsed. I want only <menu>-><footer>-> <a class="vote-up ..."> Console.Write("found") is not executed. So webBrowser can not even find <menu>

solved

Just use tricky JS

var elements=document.getElementsByClassName('vote-up');for (index = 0; index < elements.length; index++) {elements[index].click();}  

Upvotes: 0

Views: 101

Answers (1)

pythoner
pythoner

Reputation: 79

solved

Just use some js and invoke it from browser

var elements=document.getElementsByClassName('vote-up');
for (index = 0; index < elements.length; index++) {elements[index].click();} 

Upvotes: 0

Related Questions