【发布时间】:2015-01-28 17:18:05
【问题描述】:
我有一个网页,它通过 json 获取数据,然后从该数据生成 html。我希望能够对 JS 生成的源代码执行 element.invokeMember("click");(webBrowser winForms 控件)。如何在 C# 中做到这一点?
我只能在 firebug 中看到源代码。
我已经做了什么:(_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
从萤火虫复制,所以一些标签被折叠。我只想要<menu>-><footer>-> <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();}
【问题讨论】:
-
我愿意,但我想看看你的代码。您需要
HtmlElement HtmlPage.Document.GetElementById()来选择正确的 HTML 元素。或者其他可以通过Document执行的函数。 -
有很多问题询问如何等到 JS 完成 - 也可以随意寻找一个,如果它不起作用,请尝试更新这个问题(带有代码/原始链接)跨度>
-
可以,但是source是JS生成的,getElementById()不起作用。
-
您是否像 Alexei Levenkov 提到的那样等待 JS 完成?
-
是的,我发现:pastebin.com/HwWN4Nqe 并且不起作用。 _ie 从这里抓取:stackoverflow.com/questions/9776359/…(答案在底部)
标签: javascript c# winforms webbrowser-control