我正在使用for循环来选择多个复选框。它选择 复选框,但是如果我选择索引值1,然后运行代码,它将选择1值,但跳2值并选择3个复选框。
@FindBy(xpath="//li[@class='px-2']")
List <WebElement> listofitems;
for(int i=1; i<=5; i++){
listofitems.get(i).click();
System.out.println(i);
}
答案 0 :(得分:0)
Use the below code
for(int i=0; i<=5; i++){
listofitems.get(i).click();
System.out.println(i);
}
答案 1 :(得分:0)
如果您想单击多个复选框。然后尝试在元素之间进行迭代并执行click选项。如果您的元素标识符中包含任何文本,请使用类似的
List<WebElement> checkboxes = driver.FindElements(By.XPath("//li[@class='px-2']"));
foreach(WebElement chkBox in checkboxes)
{
if(chkBox.Text.Contains("checkBoxName"))
{
chkBox.Click();
}
}`
否则,如果您想单击所有复选框,则只需使用不带if条件的
foreach(WebElement chkBox in checkboxes)
{
chkBox.Click();
}