From the course: Test Automation with Selenium WebDriver for Java
Unlock this course with a free trial
Join today to access over 24,800 courses taught by industry experts.
Checkboxes
From the course: Test Automation with Selenium WebDriver for Java
Checkboxes
- [Instructor] Checkboxes can be selected or deselected simply by clicking them. However, you should check the state of a checkbox before calling the click method so that you don't inadvertently take the opposite action. For example, if your goal is to check this checkbox, but it's already checked, when you call the click method, it will uncheck the element. Let's explore how to properly interact with checkboxes with Web Driver. Let's first create a web element object representing the checkbox. So we'll say WebElement checkbox = driver.findElement(By.id("my-check-1")). Web Driver provides an is selected method that returns true if the element is selected and false if not. Let's call that method. So we'll say boolean isSelected = checkbox.isSelected(). Now we'll use an if statement to determine if the checkbox needs to be clicked in order to meet our objective. So we'll say, if(!isSelected), meaning the checkbox is not checked, then we'll go ahead and click the checkbox to select it…