C#Watin返回的TD数据为空
本文关键字:数据 TD 返回 C#Watin | 更新日期: 2023-09-27 18:24:57
我正在使用Watin从密码同步页面检索数据。我希望返回值。如果值为 (在HTML代码中)。
但我得到了其他更意想不到的
{string[4, 4]}
[0, 0] = null
[0, 1] = "MAINFRAME"
[0, 2] = "DOMAIN"
[0, 3] = "WebClient"
[1, 0] = null
[1, 1] = "INTERNALID"
[1, 2] = "FIRSTNAME.LASTNAME"
[1, 3] = "INTERNALID"
[2, 0] = null
[2, 1] = "Yes"
[2, 2] = "Yes"
[2, 3] = "Yes"
[3, 0] = null
[3, 1] = ""
[3, 2] = ""
[3, 3] = ""
HTML代码看起来像
<TABLE WIDTH="100%" CELLSPACING=0 CELLPADDING=5 BORDER=0 class="TABLEBORDER">
<TR>
<TH class=HEADTEXT>
Target
</TH>
<TH class=HEADTEXT>
Trigger<br>enabled?
</TH>
<TH class=HEADTEXT>
Account
</TH>
<TH class=HEADTEXT>
Password<br>changed?
</TH>
<TH class=HEADTEXT>
Error message<br>(if any)
</TH>
<!-- S_STATUS_ROW -->
<TR>
<TD valign=top class=CELLTEXT>
MAINFRAME
</TD>
<TD valign=top class=CELLTEXT>
No
</TD>
<TD valign=top class=CELLTEXT>
INTERNALID
</TD>
<TD valign=top class=CELLTEXT>
Yes
</TD>
<TD valign=top class=CELLTEXT>
</TD>
<!-- S_STATUS_ROW -->
<TR>
<TD valign=top class=CELLTEXT>
DOMAIN
</TD>
<TD valign=top class=CELLTEXT>
No
</TD>
<TD valign=top class=CELLTEXT>
FIRSTNAME.LASTNAME
</TD>
<TD valign=top class=CELLTEXT>
Yes
</TD>
<TD valign=top class=CELLTEXT>
</TD>
<!-- S_STATUS_ROW -->
<TR>
<TD valign=top class=CELLTEXT>
WebClient
</TD>
<TD valign=top class=CELLTEXT>
No
</TD>
<TD valign=top class=CELLTEXT>
INTERNALID
</TD>
<TD valign=top class=CELLTEXT>
Yes
</TD>
<TD valign=top class=CELLTEXT>
</TD>
<!-- S_STATUS_CONTENT_BOTTOM -->
</TABLE>
最后是有问题的C#代码。
String path = "http://localhost/cgi.exe?ID=" + userName;
Int32 startingRow = 1;
using (var browser = new IE(path))
{
try
{
browser.Image(Find.ByAlt("Use a password")).Click();
browser.TextField(Find.ByName("_MYPW")).TypeText(privateCurrentPassword);
browser.Image(Find.ByAlt("Verify password")).Click();
browser.Link(Find.ByTitle("Change passwords")).Click();
browser.TextField(Find.ByName("_NEWP1")).TypeText(privateNewPassword);
browser.TextField(Find.ByName("_NEWP2")).TypeText(privateNewPassword);
browser.Image(Find.ByName("SUBMIT-CHANGE")).Click();
Table table = browser.Table(Find.ByClass("TABLEBORDER"));
Int32 numberOfColumn = table.TableRows[startingRow].TableCells.Count;
Int32 currentRow = startingRow;
td1stcolumn = new String[table.OwnTableRows.Count];
td2ndcolumn = new String[table.OwnTableRows.Count];
td3rdcolumn = new String[table.OwnTableRows.Count];
td4thcolumn = new String[table.OwnTableRows.Count];
for (int i = 1; i < table.OwnTableRows.Count; i++)
{
td1stcolumn[i] = table.OwnTableRows[i].TableCells[0].Text.Trim();
td2ndcolumn[i] = table.OwnTableRows[i].TableCells[2].Text.Trim();
td3rdcolumn[i] = table.OwnTableRows[i].TableCells[3].Text.Trim();
td4thcolumn[i] = table.OwnTableRows[i].TableCells[4].Text.Trim();
}
String[,] arrayOfStrings = new String[4,td1stcolumn.Length];
for(int j = 0; j < td1stcolumn.Length; j++)
{
arrayOfStrings[0,j] = td1stcolumn[j];
arrayOfStrings[1,j] = td2ndcolumn[j];
arrayOfStrings[2,j] = td3rdcolumn[j];
arrayOfStrings[3,j] = td4thcolumn[j];
}
string resultDialogString = ResultDialog.ShowBox(arrayOfStrings, "Result Page", passwordTextBox.Text.ToString().Trim());
}
catch (ElementNotFoundException enfe)
{
String error = enfe.Message.ToString();
browser.TextField(Find.ByName("_MYPW")).TypeText(privateCurrentPassword);
browser.Image(Find.ByAlt("Verify password")).Click();
browser.Link(Find.ByTitle("Change passwords")).Click();
browser.TextField(Find.ByName("_NEWP1")).TypeText(privateNewPassword);
browser.TextField(Find.ByName("_NEWP2")).TypeText(privateNewPassword);
browser.Image(Find.ByName("SUBMIT-CHANGE")).Click();
Table table = browser.Table(Find.ByClass("TABLEBORDER"));
Int32 numberOfColumn = table.TableRows[startingRow].TableCells.Count;
Int32 currentRow = startingRow;
td1stcolumn = new String[table.OwnTableRows.Count];
td2ndcolumn = new String[table.OwnTableRows.Count];
td3rdcolumn = new String[table.OwnTableRows.Count];
td4thcolumn = new String[table.OwnTableRows.Count];
for (int i = 1; i < table.OwnTableRows.Count; i++)
{
td1stcolumn[i] = table.OwnTableRows[i].TableCells[0].Text.Trim();
td2ndcolumn[i] = table.OwnTableRows[i].TableCells[2].Text.Trim();
td3rdcolumn[i] = table.OwnTableRows[i].TableCells[3].Text.Trim();
td4thcolumn[i] = table.OwnTableRows[i].TableCells[4].Text.Trim();
}
String[,] arrayOfStrings = new String[4, td1stcolumn.Length];
for (int j = 0; j < td1stcolumn.Length; j++)
{
arrayOfStrings[0, j] = td1stcolumn[j];
arrayOfStrings[1, j] = td2ndcolumn[j];
arrayOfStrings[2, j] = td3rdcolumn[j];
arrayOfStrings[3, j] = td4thcolumn[j];
}
string resultDialogString = ResultDialog.ShowBox(arrayOfStrings, "Result Page", passwordTextBox.Text.ToString().Trim());
}
}
这里的问题是什么?你期望得到什么?你得到的一切都可以很简单地解释,我会努力解决一些明确的问题。
首先,你得到的空值是因为
tdNstcolumn = new String[table.OwnTableRows.Count];
和环路
for (int i = 1; i < table.OwnTableRows.Count; i++)
以i = 1
开始,这意味着tdNst列的第一个(索引0)从未被填充,因此为null(当复制到arrayOfStrings时,循环以j = 0
开始)。
您获得的"(空字符串)值来自
<TD valign=top class=CELLTEXT>
</TD>
nbsp,根据定义,是一个不可破坏的空间,即",它由您对Trim()
的调用修剪Trim函数修剪的不仅仅是",它还修剪了所有被视为空白的字符->新行也是空白(请参阅:http://msdn.microsoft.com/en-us/library/t809ektx.aspx)
我希望它能让一切都清楚