【问题标题】:Check box Click Method issue ( similar xpath) - WebDriver using java复选框单击方法问题(类似 xpath) - 使用 java 的 WebDriver
【发布时间】:2014-11-25 12:32:30
【问题描述】:

Div 内的两个不同 Div 有一个复选框,所以我想单击复选框(即在“divPatPortfolioStatusCount”内),两个复选框 xpath 相似

(i.e By.xpath("//input[@accesskey='2']")

HTML 代码

<div id="divCreatePortfolio" class="wrapper">
<table class="adminlistfilter" width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr data-bind="if: RowCounts>0, attr: {PortfolioId: Id, DescName:Name}" portfolioid="2" descname="Client-Default">
<td style="width: 5%;">
<input type="checkbox" data-bind="attr: { accesskey: Id }" accesskey="2">
</td>
</tr>
</tbody>
</table>
</div>
<div id="divPatPortfolioStatusCount" class="wrapper">
<table class="adminlistfilter" width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr data-bind="if: RowCounts>0, attr: {StatusId: Id, DescName:Name}" statusid="2" descname="Abandoned">
<td style="width: 5%;">
<input type="checkbox" data-bind="attr: { accesskey: Id }" accesskey="2">
</td>
</tr>
</tbody>
</table>
</div>
</div>

我的 Java 代码

WebElement statusDiv= driver.findElement(By.id("divPatPortfolioStatusCount"));
WebElement checkBox = statusDiv.findElement(By.xpath("//input[@accesskey='2']"));
checkBox.click();

在“divCreatePortfolio”复选框下执行时,只检查了“divPatPortfolioStatusCount”,让我知道我的 xpath 的问题

【问题讨论】:

    标签: java jquery selenium xpath selenium-webdriver


    【解决方案1】:

    您需要分别点击右下方的这两个不同的复选框吗?

    //To check Status checkbox
    driver.findElement(By.xpath("//div[@id='divCreatePortfolio']//input")).click();
    //To check Status count checkbox
    driver.findElement(By.xpath("//div[@id='divPatPortfolioStatusCount']//input")).click(); 
    

    【讨论】:

    • 谢谢,但假设它有 'n' 个复选框意味着每个 div 如何使用上面的 xpath(复选框是动态的)所以只有我用 id 框住 xpath(即 accesskey="2")
    • 没有特别的 Checkbox ,取决于我的测试脚本,
    • 然后传递“div” id 作为参数来点击所需的复选框。
    【解决方案2】:

    我建议你使用cssnth-child() 函数并通过测试控制child index

    body>div:nth-child(1) input
    body>div:nth-child(2) input
    

    nth-child(1)的数量从1改为2会发现连续的复选框

    【讨论】:

      【解决方案3】:

      在我的代码中更新 xpath

      WebElement statusTable = driver.findElement(By
                      .xpath("//*[@id='divPatPortfolioStatusCount']/table/tbody"));
              List<WebElement> rows = statusTable.findElements(By.tagName("tr"));
              for (int i = 0; i < rows.size(); i++) {
                  WebElement checkBoxSts = driver
                          .findElement(By
                                  .xpath("//*[@id='divPatPortfolioStatusCount']/table/tbody/tr["
                                          + i + "]"));
                  String statusAccessKey = checkBoxSts.getAttribute("statusid");
      
                  if (statusAccessKey.equals(portfolioId)) {
                      WebElement checkbox = driver
                              .findElement(By
                                      .xpath("//*[@id='divPatPortfolioStatusCount']/table/tbody/tr["
                                              + i + "]/td[1]/input"));
                      checkbox.click();
                      break;
                  }           
              }
      

      从数据库中获取statusid对应的status,并在这个方法中带参数传递statusid,然后继续

      【讨论】:

        【解决方案4】:

        你可以这样做:

        List<WebElements> lst = driver.findElements(By.xpath("//input[@accesskey='2']"));
        for (WebElement web : lst)
            if(!web.isSelected())
                web.click();
        

        如果你想根据 div id 选择输入,你可以使用:

        //div[@id='divPatPortfolioStatusCount']//input
        

        【讨论】:

        • 解决方案是如果你想用 xpath 检查所有复选框://input[@accesskey='2']
        猜你喜欢
        • 1970-01-01
        • 2018-04-24
        • 1970-01-01
        • 2015-12-23
        • 2016-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多