简体   繁体   中英

select checkbox using selenium webdriver

i need to select checkbox which is having same ids 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. like this But here my selenium code is clickin on 1st checkbox from the list of checkboxes as shown in image 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:

    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. 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();

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.

Checkbox appearing just before the text box can be selected using the preceding xpath function. 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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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