日期:2014-05-17  浏览次数:20856 次

老问题:webbrowser如何模拟点击

        <a id="text_ddlOFmt" href="javascript:void(0)" class="ddl-text dropdown odds-type">A股</a><input name="ddlOFmt" id="ddlOFmt" value="2" type="hidden">
        <a class="timer" href="javascript:void(0)" title="刷新">28</a>////点击这里

如何才能实现,模拟点击刷新呢
    

------解决方案--------------------
按钮有ID最好了,可以用ID取到Element

private void Form1_Load(object sender, EventArgs e)
{
this.wb.Url = new Uri("https://login.yahoo.com/config/login_verify2?.intl=gr&.src=ym");
this.wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
public void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlDocument doc = wb.Document;
  HtmlElement goButton= doc.GetElementById("submit");
goButton.InvokeMember("click");


如果没有ID,可以通过class来取得

例如:注意,如果多个element使用同样的class,都会返回。

static IEnumerable<HtmlElement> ElementsByClass(HtmlDocument doc, string className)
{
  foreach (HtmlElement e in doc.All)
    if (e.GetAttribute("className") == className)
      yield return e;
}


------解决方案--------------------
for (int i = 0; i < webBrowser1.Document.All.Count; i++)
  {
  if (webBrowser1.Document.All[i].TagName == "A" && webBrowser1.Document.All[i].GetAttribute("href").ToString().Trim() == "")
{
  webBrowser1.Document.All[i].InvokeMember("click");
  break;
  }
  }