System.Reflection.TargetInvocationException 错误 (C#)

System.Reflection.TargetInvocationException error (C#)

我正在尝试使用 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.TargetInvocationException:'An exception has been raised by the target of an appeal.'

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
 }