【问题标题】:select checkbox using selenium webdriver使用 selenium webdriver 选择复选框
【发布时间】:2017-06-09 22:08:07
【问题描述】:

我需要选择具有相同 ID 的复选框 HTML:

<label class="table-checkbox-label" for="record-12034">
<input id="record-12034" class="table-checkbox" type="checkbox"/>
</label>
</td>
<td>91363007</td>
<td>EC4N</td>
<td>true</td>
<td>ACTIVE</td>
</tr>
<tr data-id="12201">
<td>
<label class="table-checkbox-label" for="record-12201">
<input id="record-12201" class="table-checkbox" type="checkbox"/>
</label>
</td>

list of checkboxes。 我会将合同 ID 发送到合同 ID 下面的过滤框,就像一个自动完成框。所以它会根据输入给出结果。 like this 但在这里我的硒代码是单击复选框列表中的第一个复选框,如图 1 所示。

请给点建议

编辑: : 关键字函数代码:

public void filter(String objectName,String testData) throws InterruptedException,IOException {

WebDriverWait wait = new WebDriverWait(driver, 15);

          wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='content']/div[7]/table/thead/tr[2]/td[2]/input")));//wait for textbox

          driver.findElement(By.xpath(".//*[@id='content']/div[7]/table/thead/tr[2]/td[2]/input")).sendKeys(testData);//send data to textbox
          System.out.print("Text box is now visible");
          driver.findElement(By.xpath(".//*[starts-with(@id,'record')]")).click();//click on checkbox 
          driver.findElement(By.xpath(".//*[@id='content']/div[7]/table/thead/tr[2]/td[2]/input")).clear();//clear textbox
        }

excel文件读取代码:

    public void ASRTaccounts() throws IOException, InterruptedException, EncryptedDocumentException, InvalidFormatException, AWTException {
                keyword = new keywords();
                 List<String> data = new ArrayList<String>();
                    File file = new File("C:\\RDMT test\\rdmt_test\\RDMT_\\LeadSuite.xlsx");
                    Workbook workbook  = WorkbookFactory.create(file);
                    DataFormatter formatter = new DataFormatter();
                    Sheet sheet = workbook.getSheet("login");
                    for (Row row : sheet) {
                       for (Cell cell : row) {
                          data.add(formatter.formatCellValue(cell));
                       }
                    }
                System.out.println(data);
                for (int i=3;i<data.size();i++){

                    if (data.get(i).equals("filter")){
                        String key = (String) data.get(i);
                        String testData = (String) data.get(i+1);
                        String objectName = (String) data.get(i+2);
                        System.out.println(key);
                        System.out.println(testData);
                        System.out.println(objectName);
                        keyword.filter(objectName,testData);

                    }

【问题讨论】:

  • 所以问题是您没有找到正确的复选框?你能告诉我们你的代码吗?你不能做driver.getElement(By.id("record-12034")) 或类似的事情吗?
  • @ToddSewell 我正在使用我正在使用 Apache POI 的 Excel 文件将合同 ID 发送到文本框。我有一组数据要发送,我需要选中复选框,然后需要清除过滤器框。
  • @LolCoder아카쉬 我尝试过使用 xpath 开始,列表被选中。问题是它正在选择复选框,但在我将值发送到文本框之前,也是第一个复选框。
  • 我提出的解决方案有什么问题?再说一次,你的 Java 代码在哪里?
  • @ToddSewell 这是我的代码,它运行良好,只有我不知道之后它开始表现得像这个问题。我已经为关键字函数添加了有问题的代码并读取了 excel 文件。

标签: java selenium selenium-webdriver automated-tests


【解决方案1】:

问题在于选择要单击的复选框。

driver.findElement(By.xpath(".//*[starts-with(@id,'record')]")).click();

这将找到在其 ID 中具有“记录”的所有元素(在您的情况下这些都是复选框)并仅返回第一个。 您需要使用类似driver.findElement(By.xpath(".//input[@id='record-idofcheckboxyouwanttoclick']")).click();

的方式选择要单击的确切复选框

编辑:哦,现在我知道问题出在哪里了。不确定过滤后的 HTML 是什么样子,但您应该使用 xpath 找到包含您发送的输入/数字的 &lt;td&gt; 元素,然后找到他的 &lt;label&gt; 前面的兄弟姐妹,其中包含您想要的 &lt;input&gt;(复选框)点击。

【讨论】:

  • 感谢您。但这只有在我直接在我的代码中给出时才有效。 By.xpath("//*[@id='record-12033']"))).click();.. 但我不是从 excel 文件传递​​测试数据,需要根据输入单击复选框。所以每次对于任何值我都无法修改我的代码。我有一组输入要发送到该文本框。
  • 过滤后我的 HTML 看起来和我给出的一样。我要试试你的建议。谢谢:)
【解决方案2】:

可以使用preceding xpath 函数选择文本框之前出现的复选框。如果您仍然遇到同样的问题,请尝试关注并告诉我:

public void filter(String objectName,String testData) throws   InterruptedException,IOException {

      WebDriverWait wait = new WebDriverWait(driver, 15);
      wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='content']/div[7]/table/thead/tr[2]/td[2]/input")));//wait for textbox

      driver.findElement(By.xpath(".//*[@id='content']/div[7]/table/thead/tr[2]/td[2]/input")).sendKeys(testData);//send data to textbox
      System.out.print("Text box is now visible");
      driver.findElement(By.xpath(".//*[@id='content']/div[7]/table/thead/tr[2]/td[2]/input/preceding::input[1]")).click();//click on checkbox 
      driver.findElement(By.xpath(".//*[@id='content']/div[7]/table/thead/tr[2]/td[2]/input/preceding::input[1]")).clear();//clear textbox
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    相关资源
    最近更新 更多