简体   繁体   English

如何使用 Selenium Webdriver - Java 查找网页上的复选框总数?

[英]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.

相关问题 在Java-Selenium中查找网页上的文本框总数 - Find a total number of text boxes on a web page in Java - Selenium 使用带有Java的Selenium WebDriver无法从页面上显示的总共6个类似元素中找到特定按钮 - Unable to locate a particular button from total of 6 similar elements present on the page using selenium webdriver with java 如何使用Selenium Java WebDriver选中复选框? - How to check the checkboxes using the Selenium Java WebDriver? 如何使用 selenium webdriver 计算网页上可用的图像数量? - How to count number of images available on a web page using selenium webdriver? 如何使用Selenium Webdriver Java查找表行号 - How to find the table row number using Selenium Webdriver Java 如何使用Java和Selenium WebDriver识别元素中的方括号是否存在 - How to identify if brackets in the element are present or not using Java and Selenium WebDriver 使用 Selenium WebDriver 和 java 断言 WebElement 不存在 - Assert that a WebElement is not present using Selenium WebDriver with java 无法使用Java和Selenium Webdriver单击网页上的所有链接 - Not able to click all links on a web page using Java and Selenium Webdriver 使用带有 Java 的 Selenium WebDriver 在新页面上查找 WebElement - Find a WebElement on the new page using Selenium WebDriver with Java 如何在使用带有 Java 的 Selenium WebDriver 单击网页上的上传按钮时显示的 Windows 资源管理器上单击取消 - How to click on Cancel on a windows explorer that displays when a upload button is clicked on a web page using Selenium WebDriver with Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM