如何在执行JS后通过weBbrowser控件操作网页?

时间:2015-01-28 17:18:05

标签: javascript c# winforms webbrowser-control

我有网页,通过json获取数据,然后从该数据生成html。我希望能够在JS生成的源代码上执行element.invokeMember("click");(webBrowser winForms控件)。如何在c#中做到这一点?

我只能在萤火虫中看到来源。

我已经做了什么:(_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 从firebug复制,因此一些标签被折叠。我只想要<menu> - &gt; <footer> - &gt; <a class="vote-up ..."> Console.Write("found")未执行。所以webBrowser甚至找不到<menu>

解决

使用棘手的JS

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

1 个答案:

答案 0 :(得分:0)

解决

只需使用一些js并从浏览器中调用它

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