![](/img/trans.png)
[英]Remove web element from the IList <IWebElement> using C# in Selenium
[英]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.Text
為null
,則將以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.