[英]How to find the total number of checkboxes present on web page using Selenium Webdriver - Java?
Environment: Selenium Webdriver Using Java环境:使用 Java 的 Selenium Webdriver
1) run a Search 1)运行搜索
2) after search 5 items will be displayed with 5 check-boxes against them 2) 搜索 5 个项目后将显示 5 个复选框
3) I want to get the number of check-boxes 3)我想获得复选框的数量
4) check-boxes have class name "checkbox" 4)复选框具有类名“复选框”
Please suggest请建议
Thanks !!谢谢 !!
Quickest and simplest method is to find a list of the checkbox elements by the className you've provided.最快和最简单的方法是通过您提供的 className 查找复选框元素列表。
List<WebElement> boxes = driver.findElements(By.className("checkbox"));
int numberOfBoxes = boxes.length();
If you want the number of checkboxes per search result, you'd need to loop for each result.如果您想要每个搜索结果的复选框数量,您需要为每个结果循环。
List<WebElement> results = driver.findElements(By.xpath("//relevant_xpath_from_your_html"));
for (Webelement result : results){
List<WebElement> boxes = result.findElements(By.className("checkbox"));
int numberOfBoxes = boxes.length()
}
The following will show all checkboxes present on a page以下将显示页面上存在的所有复选框
System.out.println(
driver.findElements(By.cssSelector("input[type='checkbox']")).size()
);
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
public class Checkbox {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "E:\\java\\WebDriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://rahulshettyacademy.com/AutomationPractice/");
driver.findElement(By.cssSelector("input[id='checkBoxOption1']")).click();//select checkbox 1
Assert.assertTrue(driver.findElement(By.cssSelector("input[id='checkBoxOption1']")).isSelected());//validated checkbox selection
//Thread.sleep(4000);//delay process to see the check and uncheck activity
driver.findElement(By.cssSelector("input[id='checkBoxOption1']")).click();//deselect checkbox 1
Assert.assertFalse(driver.findElement(By.cssSelector("input[id='checkBoxOption1']")).isSelected());//validated checkbox deselection
//to get checkbox counts on the page.
System.out.println("The checkbox count is "+ driver.findElements(By.cssSelector("input[type='checkbox']")).size());//select checkbox 1
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.