简体   繁体   English

使用Selenium WebDriver选择复选框

[英]select checkbox using selenium webdriver

i need to select checkbox which is having same ids HTML: 我需要选择具有相同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 . 复选框列表 i will send contract id to filterbox below the contract ID which is like a autocomplete box.so it will give result based on input. 我会将合同ID发送到合同ID下方的filterbox,就像自动完成框一样,因此它将根据输入给出结果。 like this But here my selenium code is clickin on 1st checkbox from the list of checkboxes as shown in image 1. 像这样,但是这里我的硒代码是单击复选框列表中的第一个复选框,如图1所示。

Please any suggestions 请任何建议

Edit: : code for keyword function: 编辑::关键字功能的代码:

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
        }

Code for reading excel file: 读取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);

                    }

Problem is in selecting checkbox you want to click. 问题在于选择要单击的复选框。

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

This will find ALL elements which have "record" in their ID (these are all checkboxes in your case) and return ONLY the first one. 这将查找所有ID中带有“记录”的元素(在您的案例中都是复选框),并且仅返回第一个。 You need to select exact one checkbox you want to click by using something like this driver.findElement(By.xpath(".//input[@id='record-idofcheckboxyouwanttoclick']")).click(); 您需要使用类似该driver.findElement(By.xpath(".//input[@id='record-idofcheckboxyouwanttoclick']")).click();东西来选择要单击的确切一个复选框driver.findElement(By.xpath(".//input[@id='record-idofcheckboxyouwanttoclick']")).click();

EDIT: Oh, now I see where is the problem. 编辑:哦,现在我知道问题出在哪里了。 Not sure how your HTML after filtering looks like, but you should use xpath to find <td> element which contains your inputs/numbers which you sent, and then find his <label> preceding sibling which contains <input> (checkbox) you want to click. 不确定过滤后的HTML外观如何,但应使用xpath查找包含发送的输入/数字的<td>元素,然后找到其<label>包含所需的<input> (复选框)的同级元素点击。

Checkbox appearing just before the text box can be selected using the preceding xpath function. 使用前面的 xpath函数可以选择文本框之前的复选框。 Try following and let me know, if you still face the same issue: 如果您仍然遇到相同的问题,请尝试关注并让我知道:

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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM