簡體   English   中英

System.Reflection.TargetInvocationException錯誤(C#)

[英]System.Reflection.TargetInvocationException error (C#)

我正在嘗試使用C#中的HtmlAgilityPack從Web上獲取信息(代碼中的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:不可能控制ActiveX'8856f961-340a-11d0-a96b-00c04fd705a2',無法進行線程復制(STA,單線程公寓)。

翻譯: System.Reflection.TargetInvocationException:“上訴目標提出了例外。”

ThreadStateException:無法實例化ActiveX控件'8856f961-340a-11d0-a96b-00c04fd705a2',因為當前線程不是分區線程(STA,單線程單元)。

如何擺脫這個錯誤?

將STAThreadAttribute應用於您的Main函數:

 [STAThread]
 static void Main(string[] args)
 {
     //Your code here
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM