簡體   English   中英

C#Selenium 2:從另一個IList創建一個IList

[英]C# Selenium 2: make an IList from another IList

我在一個表中有一個行列表,我想用第一個列表中的某些行創建另一個列表。

我的代碼是:

public IWebElement InvoiceTable { get { return Driver.FindElement(By.Id("MainContent_gvInvoices")); } }
public IList<IWebElement> InvoiceRows { get { return InvoiceTable.FindElements(By.CssSelector("tbody tr")); } }
public IList<IWebElement> ACInvoiceRows { get; set; }

public void test()
{
    foreach(IWebElement row in InvoiceRows)
    {
        if(row.Text.Contains("AC"))
        {
            ACInvoiceRows.Add(row);
        }
    }
    Console.WriteLine(ACInvoiceRows.Count);
}

這將引發NullReferenceExcpetion

你調用的對象是空的。

我在這里做錯了什么?

在下面的行中,您無需檢查row.Text是否為null

if (row.Text.Contains("AC"))

如果row.Textnull ,則將以NullReferenceException結尾。 而是執行此操作以檢查是否為null

if (row.Text != null && row.Text.Contains("AC"))

編輯:

也有可能未設置ACInvoiceRows 如果不是,則不能使用它,除非為其分配了實際對象,例如:

public void test()
{
  //Initiate the collection before using it.
  ACInvoiceRows = new List<IWebElement>();

  foreach(IWebElement row in InvoiceRows)
  {
      if(row.Text.Contains("AC"))
      {
          ACInvoiceRows.Add(row);
      }
  }
  Console.WriteLine(ACInvoiceRows.Count);
}

確保按照以下方式初始化驅動程序:

IWebDriver Driver = new ChromeDriver();

暫無
暫無

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

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