c# - System.Reflection.TargetInitationException 错误 (C#)

标签 c# html-agility-pack

我正在尝试使用 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

ThreadStateException: Impossible d'instancier le contrôle ActiveX '8856f961-340a-11d0-a96b-00c04fd705a2', car le thread actuel n'est pas un thread cloisonné (STA, Single-Threaded Apartment).

翻译: 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/

相关文章:

c# - 一行中的多个对象

c# - SignalR Web 应用程序从控制台集线器接收消息

c# - 仅使用 'else' 语句的 'if-else' 部分是否可以接受?

c# - XPath 从节点内的多个标签检索值

c# - 使用 HTML AGILITY PACK 更改标签内部文本

c# - SelectedItem 仅更新 WPF 数据库中的第一行

c# - 在单独的进程中从插件获取 WPF 应用程序中的 TextBox 焦点

c# - XPath 在浏览器和 HtmlAgilityPack 中给出不同的结果

c# - 使用 htmlagility pack 替换 src 值

c# - 使用 HTML Agility Pack 获取图像旁边的文本?