我了解此错误的含义。我已经阅读了其他相关问题和帖子。据我所知,我已经解决了这个问题,但仍然遇到错误。有人看到我没看到的东西吗?
public static IWebElement RetrieveObject(IWebDriver driver, string page, string pageObject)
{
int rCnt = 0;
string TablePath = "C:\\Automation Projects\\Tables\\";
string ObjectRepPath = TablePath + "ObjectRepository.xlsx";
string objpage = " ";
string objelement = " ";
string objelementType = " ";
string objlocator = " ";
IWebElement locator = null;
Application xlApp;
Workbook xlWorkBook;
Worksheet xlWorkSheet;
Range range;
xlApp = new Application();
xlWorkBook = xlApp.Workbooks.Open(ObjectRepPath);
xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
{
objpage = (string)(range.Cells[rCnt, 1] as Range).Value2;
objelement = (string)(range.Cells[rCnt, 2] as Range).Value2;
objelementType = (string)(range.Cells[rCnt, 3] as Range).Value2;
objlocator = (string)(range.Cells[rCnt, 4] as Range).Value2;
if (objpage == page && objelement == pageObject)
{
if (objelementType == "id")
{
locator = driver.FindElement(By.Id(objlocator));
return locator;
}
else if (objelementType == "name")
{
locator = driver.FindElement(By.Name(objlocator));
return locator;
}
else if (objelementType == "xpath")
{
locator = driver.FindElement(By.XPath(objlocator));
return locator;
}
else
{
return locator;
}
}
}
}
我将为每个可能的路径返回iWebElement“定位器”。我确定我缺少一些简单的东西,但是我盯着它看了太久了,看不到它。
答案 0 :(得分:1)
他们没有。您应该问自己一个问题:如果没有行,怎么办?
您必须在for循环之后添加return语句。
public static IWebElement RetrieveObject(IWebDriver driver, string page, string pageObject)
{
int rCnt = 0;
string TablePath = "C:\\Automation Projects\\Tables\\";
string ObjectRepPath = TablePath + "ObjectRepository.xlsx";
string objpage = " ";
string objelement = " ";
string objelementType = " ";
string objlocator = " ";
IWebElement locator = null;
Application xlApp;
Workbook xlWorkBook;
Worksheet xlWorkSheet;
Range range;
xlApp = new Application();
xlWorkBook = xlApp.Workbooks.Open(ObjectRepPath);
xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
{
objpage = (string)(range.Cells[rCnt, 1] as Range).Value2;
objelement = (string)(range.Cells[rCnt, 2] as Range).Value2;
objelementType = (string)(range.Cells[rCnt, 3] as Range).Value2;
objlocator = (string)(range.Cells[rCnt, 4] as Range).Value2;
if (objpage == page && objelement == pageObject)
{
if (objelementType == "id")
{
locator = driver.FindElement(By.Id(objlocator));
return locator;
}
else if (objelementType == "name")
{
locator = driver.FindElement(By.Name(objlocator));
return locator;
}
else if (objelementType == "xpath")
{
locator = driver.FindElement(By.XPath(objlocator));
return locator;
}
else
{
return locator;
}
}
}
return locator; // <---------------- !!
}