如何使它与列表或数组一起工作
本文关键字:数组 一起 工作 列表 何使它 | 更新日期: 2023-09-27 18:37:20
一切都在工作:链接。Remove(linkToClick),因为HtmlElementCollection没有"Remove"方法。
我最近在这里问了一个问题:如何让循环移动到下一个可用的 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");
}
}
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");
}
}
我还建议从计时器中停止计时器,如果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();