我需要将我的webdriver测试用例写入文本框并从下拉列表中选择一个选项,但这是错误的。
这是我的webdriver代码:
namespace OneViewTestingUsingSelenium
{
class ExcelLibary
{
private static DataTable ExcelTodataTable(string fileName)
{
//open this file and returns stream obj
FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read);
//create open xml reader using excel reader factory
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//set 1st row as column name
excelReader.IsFirstRowAsColumnNames = true;
//return dataset
DataSet result = excelReader.AsDataSet();
//got all the tables
DataTableCollection table = result.Tables;
//stores all the table into datatable
DataTable resultTable = table["Sheet1"];
//ultimately return this
return resultTable;
}
public static List<DataCollectionExcel> dataCol = new List<DataCollectionExcel>();
public static void PopulateInCollection(string fileName)
{
DataTable table = ExcelTodataTable(fileName);
for (int row = 1; row <= table.Rows.Count; row++)
{
for (int col = 0; col < table.Columns.Count; col++)
{
DataCollectionExcel dtTable = new DataCollectionExcel
{
rowNumber = row,
colName = table.Columns[col].ColumnName,
colValue = table.Rows[row - 1][col].ToString()
};
dataCol.Add(dtTable);
}
}
}
public static string ReadDataExcel(int rowNumber, string columnName)
{
try
{
string data = (from coldata in dataCol
where coldata.colName == columnName && coldata.rowNumber == rowNumber
select coldata.colValue).SingleOrDefault();
return data.ToString();
}
catch (Exception Ex)
{
return null;
}
}
}
public class DataCollectionExcel
{
public int rowNumber { get; set; }
public string colName { get; set; }
public string colValue { get; set; }
}
}
在我的webdriver测试用例中,我正在从Excel中读取用户信息并将其输入用户创建页面以创建新用户。
当它将数据输入文本框时没有问题,它可以正常工作;但当它将数据输入文本框和下拉列表时,我得到一个错误。
我尝试了以下内容:
IList<IWebElement> tit = SupportClass.driverFF.FindElements(By.Id("UserEntity_Master_Default_Title"));
int titCount = tit.Count;
for (int i = 0; i < titCount; i++)
{
if (tit[i].Text == "Mr")
{
tit[i].Click();
}
if (tit[i].Text == "Mrs")
{
tit[i].Click();
}
if (tit[i].Text == "Miss")
{
tit[i].Click();
}
}
答案 0 :(得分:0)
要从带有Select
html标记的下拉列表中选择一个值,即使手动我们使用鼠标点击进行下拉选择,也无法使用.Click
。您需要做的是使用{{1 } class
尝试以下
SelectElement
您可以使用IWebElement tit = SupportClass.driverFF.FindElement(By.Id("UserEntity_Master_Default_Title"));
SelectElement dropdownSelector = new SelectElement(tit);
dropdownSelector.SelectByText(yourExcelInputStringHere);
或index
value
编辑: 从excel获取数据并将其作为输入提供给Web表单的示例代码
使用的链接:http://toolsqa.com/automation-practice-form/
代码:
dropdownSelector.SelectByIndex(1);
dropdownSelector.SelectByValue("1");