简体   繁体   English

如何通过 Selenium 和 Java 根据值单击多个复选框

[英]How to click on multiple checkboxes based on value through Selenium and Java

I am trying to select multiple checkboxes.我正在尝试选择多个复选框。 Here is my HTML这是我的 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.这里 id 标签是随机生成的。 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")

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

相关问题 如何单击 selenium webdriver 中的多个复选框? - how to click the multiple checkboxes in selenium webdriver? 如何单击Selenium Webdriver中的多个复选框? - How to click on multiple checkboxes in selenium webdriver? 如何 select 硒(java)中的多个复选框? - How to select multiple checkboxes in selenium(java)? 如何通过 Java 使用 Selenium 单击元素 - How to click on the element using Selenium through Java 如何根据通过 Selenium 和 Java 提供的 HTML 单击 img 标签? - How to click on img tag as per the HTML provided through Selenium and Java? 如何通过Selenium WebDriver Java单击覆盖标记下的Webelement - how to click webelement under overlay tag through selenium webdriver java 如何使用Selenium和Java通过Label定位和单击单选按钮 - How to locate and click Radio Button through the Label using Selenium and Java 如何通过Selenium和Java按照HTML的方式单击文本为Continue的元素 - How to click on the element with text as Continue as per the HTML through Selenium and Java 如何通过类名识别元素并使用Selenium和Java单击它? - How to identify an element through classname and click on it using Selenium and Java? 如何通过Selenium和Java单击“停用”按钮,然后单击“激活”按钮 - How to click on the Deactivate button and then on Activate button through Selenium and Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM