硒动态遍历表

  • 本文关键字:遍历 动态 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");

我确信有一种方法可以循环访问表,以动态方式收集值,这样只需要:

字符串费用**"x"** = 驱动程序。FindElement(By.XPath("//tr//input[@id='fee']")).GetAttribute("title");

字符串 CCFee**"x"** = 驱动程序。FindElement(By.XPath("//tr/td[3]/select")).GetAttribute("title");

如果"x"增加到表的末尾,并且也应该是动态的,从第二行开始,如下所示:

字符串 Fee2 = 驱动程序。FindElement(By.XPath("//tr__[2]__//input[@id='fee']")).GetAttribute("title");

字符串 CCFee2 = 驱动程序。FindElement(By.XPath("//tr__[2]__/td[3]/select")).GetAttribute("title");

任何帮助将不胜感激。谢谢!

好吧,假设页面上只有一个表,您可以做这样的事情(未测试,或确切的语法,因为我不知道 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
}

最新更新