Selenium动态迭代表

时间:2016-02-23 21:13:17

标签: selenium

所以我一直在努力寻找一种动态方法,通过迭代遍历表并从该表中收集特定值来创建从远程服务到本地数据标签的映射。这是我试图迭代的代码,这些长度可能会改变:

        string Fee1 = driver.FindElement(By.XPath("//tr//input[@id='fees']")).GetAttribute("title");
        string CCFee1 = driver.FindElement(By.XPath("//tr/td[3]/select")).GetAttribute("title");

        string Fee2 = driver.FindElement(By.XPath("//tr[2]//input[@id='fees']")).GetAttribute("title");
        string CCFee2 = driver.FindElement(By.XPath("//tr[2]/td[3]/select")).GetAttribute("title");

        string Fee3 = driver.FindElement(By.XPath("//tr[3]//input[@id='fees']")).GetAttribute("title");
        string CCFee3 = driver.FindElement(By.XPath("//tr[3]/td[3]/select")).GetAttribute("title");

        string Fee4 = driver.FindElement(By.XPath("//tr[4]//input[@id='fees']")).GetAttribute("title");
        string CCFee4 = driver.FindElement(By.XPath("//tr[4]/td[3]/select")).GetAttribute("title");

        string Fee5 = driver.FindElement(By.XPath("//tr[5]//input[@id='fees']")).GetAttribute("title");
        string CCFee5 = driver.FindElement(By.XPath("//tr[5]/td[3]/select")).GetAttribute("title");

        string Fee6 = driver.FindElement(By.XPath("//tr[6]//input[@id='fees']")).GetAttribute("title");
        string CCFee6 = driver.FindElement(By.XPath("//tr[6]/td[3]/select")).GetAttribute("title");

我确信有一种方法可以迭代遍历表格,以动态方式收集值,这样就应该只需要:

string Fee **“x”** = driver.FindElement(By.XPath(“// tr // input [@ id ='fees']”))。GetAttribute(“title”);

string CCFee **“x”** = driver.FindElement(By.XPath(“// tr / td [3] / select”))。GetAttribute(“title”);

其中“x”增加到表的末尾,而且,也应该是动态的,从第二行开始,如下:

string Fee2 = driver.FindElement(By.XPath(“// tr __ [2] __ // input [@ id ='fees']”))。GetAttribute(“title”);

string CCFee2 = driver.FindElement(By.XPath(“// tr __ [2] __ / td [3] / select”))。GetAttribute(“title”);

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

好吧,假设你在页面上只有一个表,你可以做这样的事情(没有测试,或者确切的语法,因为我不知道C#):

List<WebElement> allRows = driver.FindElement(By.XPath("//tr"));
int RowIndex = 0;
for (aRow in allRows) {
    fee = aRow.FindElement(By.XPath("/input[@id='fees']")).GetAttribute("title");
    ccFee = aRow.FindElementBy.XPath("/td[3]/select")).GetAttribute("title")
    RowIndex += RowIndex;
    // store your fee and ccFee in some map, or some other structure
    // based on the RowIndex
}