c# - Selenium - System.ArgumentNullException:文本不能为空参数名称:文本
问题描述
当我尝试运行代码时,我收到此错误“System.ArgumentNullException:文本不能为空参数名称:文本”它不会从 excel 文件中提取数据;谁能给我建议如何解决这个问题?
这是脚本
ExcelLib.cs
using ExcelDataReader;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
namespace Driven
{
public class ExcelLib
{
public static DataTable ExcelToDataTable(string fileName)
{
//open file and returns as Stream
FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read);
{
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
var result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = (data) => new ExcelDataTableConfiguration()
{
UseHeaderRow = true
}
});
//Get all the Tables
DataTableCollection table = result.Tables;
//Store it in DataTable
DataTable resultTable = table["Sheet1"];
//return
return resultTable;
}
}
}
static List<Datacollection> dataCol = new List<Datacollection>();
public static void PopulateInCollection(string fileName)
{
DataTable table = ExcelToDataTable(fileName);
//Iterate through the rows and columns of the Table
for (int row = 1; row <= table.Rows.Count - 1; row++)
{
for (int col = 0; col <= table.Columns.Count; col++)
{
Datacollection dtTable = new Datacollection()
{
rowNumber = row,
colName = table.Columns[col].ColumnName,
colValue = table.Rows[row - 1][col].ToString()
};
//Add all the details for each row
dataCol.Add(dtTable);
}
}
}
public static string ReadData(int rowNumber, string columnName)
{
try
{
//Retriving Data using LINQ to reduce much of iterations
string data = (from colData in dataCol
where colData.colName == columnName && colData.rowNumber == rowNumber
select colData.colValue).SingleOrDefault();
//var datas = dataCol.Where(x => x.colName == columnName && x.rowNumber == rowNumber).SingleOrDefault().colValue;
return data.ToString();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
}
public class Datacollection
{
public int rowNumber { get; set; }
public string colName { get; set; }
public string colValue { get; set; }
}
}
}
单元测试1.cs
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using NUnit.Framework;
namespace Driven
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
ExcelLib.PopulateInCollection(@"D:\Data.xlsx");
IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("");
driver.FindElement(By.Id("UserName")).SendKeys(ExcelLib.ReadData(1, "getUserNamefromexcel"));
driver.FindElement(By.Id("Password")).SendKeys(ExcelLib.ReadData(1, "getPasswordfromexcel"));
driver.FindElement(By.Name("Action")).Click();
}
}
}
运行方法 UnitTest1 后出错
消息:测试方法驱动。UnitTest1.TestMethod1 抛出异常:System.ArgumentNullException:文本不能为空参数名称:文本
调试方法 UnitTest1 后出错
WebDriver.dll 中出现“System.ArgumentNullException”类型的异常,但未在用户代码中处理
解决方案
根据错误消息,ExcelLib.ReadData()
返回 null。Null 是不可接受的SendKeys
。
您正在返回null
异常。这可能是原因之一。
检查数据是否正确填充。然后检查数据是否可读。此外,在 read 方法中打印异常的堆栈跟踪。
推荐阅读
- mysql - 带有like查询的MySql计数响应非常慢
- php - 如何在laravel中计算付款和发票页面中的余额?
- windows-subsystem-for-linux - Windows上的basexclient无法连接到WSL中的BaseX服务器
- powershell - Connect-MicrosoftTeams OAuth 无效的受众错误
- java - Android Java 数据未显示在 TableView 中
- python - 当我们在循环中插入新行时,如何遍历电子表格中的所有行?
- c# - 在 GridView 中删除行时出错
- android - 相机点击 Xamarin Force Portrait
- python - 过滤具有精确字符串匹配的数据框
- nuxt.js - 如何以与@nuxt/apollo subscribeToMore 一起使用的方式设置我的 nuxt