我正在尝试使用 C# 中的 HtmlAgilityPack 从网络(我的代码中的 adress
)获取信息,但我必须等到 <div class="center fs22 green lh32">
已加载到页面上。
当我执行这段代码时:
var url = $"https://www.website.com/";
var web = new HtmlWeb();
var doc = web.LoadFromBrowser(url, html =>
{
return !html.Contains("<div class=\"center fs22 green lh32\"></div>");
});
string adress = doc.DocumentNode
.SelectSingleNode("//td/span[@id='testedAddress")
.Attributes["value"].Value;
Console.WriteLine("Voici l'adresse :",adress);
我总是收到此错误:
翻译: System.Reflection.TargetInitationException:“申诉目标引发了异常。”
ThreadStateException: Can not instantiate ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2', because the current thread is not a partitioned thread (STA, Single-Threaded Apartment).
如何消除这个错误?
最佳答案
将 STAThreadAttribute 应用于您的 Main 函数:
[STAThread]
static void Main(string[] args)
{
//Your code here
}
关于c# - System.Reflection.TargetInitationException 错误 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49046093/