0

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();}  
Community
  • 1
  • 1
pythoner
  • 79
  • 1
  • 7
  • 1
    I do, but I would like to see your code. You need `HtmlElement HtmlPage.Document.GetElementById()` to select the correct HTML element. Or other function that can be executed via `Document`. – Mouser Jan 28 '15 at 17:25
  • There are plenty of questions that ask how to wait till JS is done - feel free too look for one, try and update this question if it did not work (with code/link to original) – Alexei Levenkov Jan 28 '15 at 17:26
  • Yes, but source is generated by JS, and getElementById() does not work. – pythoner Jan 28 '15 at 17:27
  • Did you wait for the JS to finish as Alexei Levenkov mentioned? – Erik Philips Jan 28 '15 at 17:29
  • Yes, I found that: http://pastebin.com/HwWN4Nqe And does not work. _ie grabbed from here: http://stackoverflow.com/questions/9776359/how-to-make-webbrowser-wait-till-it-loads-fully (answer on the bottom) – pythoner Jan 28 '15 at 17:35
  • I call you @AlexeiLevenkov – pythoner Jan 28 '15 at 17:43
  • @pythoner If you found your answer post the answer of question in the answer field. – Ali Vojdanian Jan 28 '15 at 20:17

1 Answers1

0

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();} 
Community
  • 1
  • 1
pythoner
  • 79
  • 1
  • 7