1

I am trying to select multiple checkboxes. Here is my HTML

<div class="col-md-6">
   <div class="form-sections">
      <ul>
         <li>Select permissions</li>
         <li><input type="checkbox" id="permissions1549733530963" name="permissions"><label for="permissions1549733530963">Select all</label></li>
      </ul>
      <div class="searchbox-container">
         <div class="check-list">
            <ul>
               <li><input type="checkbox" id="371549733530963" name="permissions" value="Add User"><label for="371549733530963">Add User</label></li>
               <li><input type="checkbox" id="31549733530965" name="permissions" value="View User"><label for="31549733530965">View User</label></li>
            </ul>
         </div>
      </div>
   </div>
</div>

I want to select only two checkbox. I'm doing this:

driver.findElement(By.xpath("//input[@type='checkbox' && @name='permissions' && @value='"+value+'"")).click();

here id tag is generated randomly. How to select multiple checkboxes based on the value tag?

2 Answers 2

1

By Using string array I did this. Please try.

String[] users = {"Add/Update Network Security", "Create User"};
int size = users.length;
for (int i=0; i<size; i++)
{
    String value=users[i];
    System.out.println(value);

    WebDriverWait wait = new WebDriverWait(driver, 30);
           wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@type='checkbox' and @value='" +value +"']"))).click();               
}

Or

String[] users = {"Add/Update Network Security", "Create User"};
int size = users.length;
for (int i=0; i<size; i++)
{
    String value=users[i];
    System.out.println(value);
    WebElement element=driver.findElement(By.xpath("//input[@type='checkbox' and @value='" +value +"']"));
               JavascriptExecutor js = (JavascriptExecutor) driver;
               js.executeScript("arguments[0].click();", element);                           
}
Sign up to request clarification or add additional context in comments.

7 Comments

didn't worked same error org.openqa.selenium.ElementNotVisibleException: element not interactable
I have added some wait to visible of your element.can you try that.
If your application is slow you can increase the wait time.
I tired 50 instead of 30. it says org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //input[@type='checkbox' and @value='Add Network'] (tried for 50 second(s) with 500 milliseconds interval)
Ok.Try this one I have trired with javascripts executor.
|
0

To click on any of the check boxes you can create a function as follows:

public void locateClickCheckbox(String item)
{
    String myElement = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='form-sections']//div[@class='check-list']//li/input[@value='" + item + "']"))).getAttribute("id");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='form-sections']//div[@class='check-list']//li/label[@for='" + myElement + "']"))).click();
}

Now you can call the function from anywhere within your program as:

locateClickCheckbox("Add/Update Network Security")
locateClickCheckbox("Create User")
locateClickCheckbox("Create Project")
locateClickCheckbox("Update User Details")
locateClickCheckbox("View User")
locateClickCheckbox("Assign Permissions")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.