【问题标题】:Selenium dynamically iterate through tableSelenium 动态遍历表
【发布时间】:2016-06-05 22:10:56
【问题描述】:

因此,我一直在努力寻找一种通过 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");

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

【问题讨论】:

    标签: selenium


    【解决方案1】:

    好吧,假设您在页面上只有一个表格,您可以执行以下操作(未经测试,或者因为我不懂 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
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-09
      • 2016-05-29
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 2020-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多