-3

除此之外一切正常: links.Remove(linkToClick) 因为 HtmlElementCollection 没有“删除”方法。

我最近在这里问了一个问题:如何让循环移动到下一个可用的 id 而不是连续做同样的事情?

这就是向我推荐的,但在过去的两个小时里我一直在寻找并试图弄清楚,但仍然无法弄清楚。

HtmlElementCollection links = null;

private void button2_Click(object sender, EventArgs e)
{
    // This way you only get the links once.
    links = webBrowser1.Document.GetElementsByTagName("a");

    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    HtmlElement linkToClick = null;

    foreach (HtmlElement link in links)
    {
        if (link.GetAttribute("id").Contains("user"))
        {
            linkToClick = link;
            break;
        }
    }

    // did I find a link?
    if (linkToClick != null)
    {
        // Remove it from the list so you don't click it again.
        links.Remove(linkToClick);

        link.InvokeMember("click");
    }
}
4

1 回答 1

2
List<HtmlElement> links = null;

private void button2_Click(object sender, EventArgs e)
{
    // This way you only get the links once.
    links = new List<HtmlElement>(webBrowser1.Document.GetElementsByTagName("a")
                                                      .Cast<HtmlElement>());

    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    HtmlElement linkToClick = null;

    foreach (HtmlElement link in links)
    {
        if (link.GetAttribute("id").Contains("user"))
        {
            linkToClick = link;
            break;
        }
    }

    // did I find a link?
    if (linkToClick != null)
    {
        // Remove it from the list so you don't click it again.
        links.Remove(linkToClick);

        linkToClick.InvokeMember("click");
    }
}

我还建议从计时器中停止计时器 if linkToClick == null,为什么你不提前过滤列表?

links = new List<HtmlElement>(webBrowser1.Document.GetElementsByTagName("a")
                                                  .Cast<HtmlElement>());

links.RemoveAll(link => !link.GetAttribute("id").Contains("user"));

在计时器中:

if (links.Count > 0)
{
    links[0].InvokeMember("click");
    links.RemoveAt(0);
}
// else timer.Stop();
于 2012-06-14T04:38:09.733 回答