Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用SeleniumWebDriver获取基于目标行的表格单元格文本_C#_Selenium Webdriver - Fatal编程技术网

C# 如何使用SeleniumWebDriver获取基于目标行的表格单元格文本

C# 如何使用SeleniumWebDriver获取基于目标行的表格单元格文本,c#,selenium-webdriver,C#,Selenium Webdriver,我试图根据单元格索引和行标识符获取表单元格的文本。在下面的示例表中,我希望得到与单元格“text6”位于同一行的单元格文本。以下是我正在处理的表格示例: <table cellspacing="0" cellpadding="0" class="fixedTable" id="PackageTable_dataTable" style="width: 1262px;"> <tbody class="tableData" id="PackageTable_dataBody">

我试图根据单元格索引和行标识符获取表单元格的文本。在下面的示例表中,我希望得到与单元格“text6”位于同一行的单元格文本。以下是我正在处理的表格示例:

<table cellspacing="0" cellpadding="0" class="fixedTable" id="PackageTable_dataTable" style="width: 1262px;">
<tbody class="tableData" id="PackageTable_dataBody">
<tr height="24px" uid="section1" index="0" class="alternateRow">
<td><div style="overflow: hidden; width: 193px;"><span title="text1">text1</span></div></td>
<td><div style="overflow: hidden; width: 57px;"><span title="text2">text2</span></div></td>
<td><div style="overflow: hidden; width: 115px;"><span title="text3">text3</span></div></td>
</tr>
<tr height="24px" uid="section2" index="1" class="alternateRow">
<td><div style="overflow: hidden; width: 193px;"><span title="text4">text4</span></div></td>
<td><div style="overflow: hidden; width: 57px;"><span title="text5">text5</span></div></td>
<td><div style="overflow: hidden; width: 115px;"><span title="text6">text6</span></div></td>
</tr>
<tr height="24px" uid="section3" index="2" class="alternateRow">
<td><div style="overflow: hidden; width: 193px;"><span title="text7">text7</span></div></td>
<td><div style="overflow: hidden; width: 57px;"><span title="text8">text8</span></div></td>
<td><div style="overflow: hidden; width: 115px;"><span title="text9">text9</span></div></td>
</tr>
</tbody>
</table>

文本1
文本2
文本3
文本4
文本5
文本6
文本7
文本8
文本9
这是我的非工作代码:

public static string returnTableCellValue(string TableID, string TableRowIdentifier, int targetCellIndex)
        {
            string cellValue = string.Empty;
            try
            {
                IWebElement baseTable = Global.driver.FindElement(By.Id(TableID));
                // gets all table rows
                ICollection<IWebElement> rows = baseTable.FindElements(By.TagName("tr"));
                // for every row
                foreach (IWebElement row in rows)
                {
                    if (row.FindElement(By.XPath("//span[text()='" + TableRowIdentifier + "']")).Displayed)
                    {
                        Global.log.WriteLine("row identifier found!");
                        IWebElement key = row.FindElement(By.XPath("//td[" + targetCellIndex + "]"));
                        IWebElement keySpan = key.FindElement(By.TagName("span"));
                        cellValue = keySpan.Text;
                    }
                }
                return cellValue;
            }

            catch (Exception ex)
            {
                Global.log.WriteLine("returnTableCellValue exception: " + ex.ToString());
                return string.Empty;
            }
        }
public静态字符串returnTableCellValue(字符串TableID、字符串TableRowIdentifier、int targetCellIndex)
{
string cellValue=string.Empty;
尝试
{
IWebElement baseTable=Global.driver.FindElement(By.Id(TableID));
//获取所有表行
ICollection rows=baseTable.FindElements(按.TagName(“tr”));
//每行
foreach(行中的IWebElement行)
{
if(row.FindElement(按.XPath(“//span[text()=”“+TableRowIdentifier+”)))。显示)
{
Global.log.WriteLine(“找到行标识符!”);
IWebElement key=row.FindElement(By.XPath(“//td[”+targetCellIndex+“]);
IWebElement keySpan=key.FindElement(按.TagName(“span”);
cellValue=keySpan.Text;
}
}
返回单元格值;
}
捕获(例外情况除外)
{
Global.log.WriteLine(“returnTableCellValue异常:+ex.ToString());
返回字符串。空;
}
}

知道怎么做吗?

代码中的下一行将在第一行抛出
NoTouchElementException
,该
FindElement
未找到具有匹配条件的元素

if (row.FindElement(By.XPath("//span[text()='" + tableRowIdentifier + "']")).Displayed)
改为使用FindElements(),如下所示:

IWebElement matchedRow = null;
try
{
    foreach(var row in rows)
    {
        if(row.FindElements(By.XPath("td/span")).FirstOrDefault(cell => cell.Text.Trim().Equals(TableRowIdentifier)) != null)
        {
            matchedRow = row;
            break;
        }
    }
}
catch (NoSuchElementException)
{
    //couldnot find 
    matchedRow = null;
}

if(matchedRow !=null)
{
    cellValue = matchedRow.FindElement(By.XPath(string.Format("td[{0}]/span",targetCellIndex)).Text;
}

return cellValue;

它怎么不起作用?你有什么错误?阿兰,谢谢你的回复。正如Faiz在下面指出的,我的代码在遇到没有标识符的第一行时会给出ElementNotFound异常。他的建议代码似乎不起作用-没有异常,但没有找到行。我的代码找不到标识符正确的唯一行。还有其他建议吗?行:row.FindElements(By.XPath(“td/span”)).FirstOrDefault(cell=>cell.Text.Trim().Equals(TableRowIdentifier))!=null未找到正确的行,因此未返回任何数据。知道为什么那一行没有返回正确的行吗?抱歉,上面的代码确实有效-我在本地测试时使用了错误的xpath表达式。非常感谢Faiz和Arran