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?
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);
}
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")
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.