我正在尝试选择多个复选框。这是我的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>


我只想选择两个复选框。我正在这样做:

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


id标签是随机生成的。如何基于值标签选择多个复选框?

最佳答案

通过使用字符串数组,我做到了这一点。请试试。

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


要么

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);
}

关于java - 如何通过Selenium和Java基于值单击多个复选框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54607734/

10-10 22:27