/html/body/table/tr[1]/td[2]
/html/body/table/tr[1]/td[4]
/html/body/table/tr[3]/td[2]
/html/body/table/tr[3]/td[4]
/html/body/table/tr[5]/td[2]
/html/body/table/tr[5]/td[4]
所以,TR指數[]是奇數,而TD []將始終是2或4如何爲以下情況創建循環?
/html/body/table/tr[1]/td[2]
/html/body/table/tr[1]/td[4]
/html/body/table/tr[3]/td[2]
/html/body/table/tr[3]/td[4]
/html/body/table/tr[5]/td[2]
/html/body/table/tr[5]/td[4]
所以,TR指數[]是奇數,而TD []將始終是2或4如何爲以下情況創建循環?
for(int i = 1; i < bound; i += 2) {
for(int j = 2; j <= 4; j += 2) {
Console.WriteLine(
String.Format("/html/body/table/tr[{0}]/td[{1}]", i, j)
);
}
Console.WriteLine();
}
打敗我吧... –
我可能會做一個關於綁定的小評論(在這種情況下,他的示例至少需要6個) – SwDevMan81
爲什麼使用兩個循環?具有兩個writeline的單個循環會更易讀,因爲兩個tr的索引都是相同的值,而td是總是2和4. –
你可以做的
for(tr = 1; tr < maxodd+1; tr += 2;)
{
//pseudoimplementation
/html/bod/table/tr[tr]/td[2]
/html/bod/table/tr[tr]/td[4]
}
最天真的案例:
for(int i = 1; i < 6; i += 2) {
Console.WriteLine("html/body/table/tr[" + i + "]/td[2]");
Console.WriteLine("html/body/table/tr[" + i + "]/td[4]");
}
你在看創建文本嗎? Ie/html/body/table/ – Jethro
@Jethro:是的。我需要做一些像Selenium.GetText(「xpath =/html/body/table/tr [1]/td [2]」)的循環。 – Maya