webbrowser InvokeMember("event or script")

        public static List<RegionInfo> GetRegionInfosFromWeb(WebBrowser ie)
{
List<RegionInfo> citys = new List<RegionInfo>();

HtmlElement element = ie.Document.All.GetElementsByName("strCity")[0];
if (element == null) return new List<RegionInfo>();

foreach (HtmlElement el in element.Children)
{
RegionInfo info = new RegionInfo();

el.SetAttribute("Selected", "true");
info.City = el.GetAttribute("Value");
element.InvokeMember("onclick");

Thread.Sleep(1000);

HtmlElement e = ie.Document.All.GetElementsByName("strDistrict")[0];
if (e == null)
{
Debug.WriteLine(string.Format("»ñÈ¡{0}µÄ×ÓµØÇø³ö´í", info.City));
continue;
}

info.SubCitys = new List<string>();
foreach (HtmlElement m in e.Children)
{
m.SetAttribute("Selected", "true");
if (!string.IsNullOrEmpty(m.GetAttribute("value")))
info.SubCitys.Add(m.GetAttribute("value"));
}

citys.Add(info);
}

return citys;
}

webbrowser 采集数据时很好用,遗憾的是不能在非UI线程上工作

原文地址:https://www.cnblogs.com/xiaowy/p/840810.html