You are reading the article How To Select Radio Button And Checkbox In Selenium updated in September 2023 on the website Dacquyenphaidep.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How To Select Radio Button And Checkbox In Selenium
In this tutorial, we will see how to identify the following form elements
Radio Button in SeleniumHow to Select Checkbox in Selenium
Complete Code
Here is the complete working code
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.*; public class Form { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","G:\chromedriver.exe"); WebDriver driver = new ChromeDriver(); WebElement radio1 = driver.findElement(By.id("vfb-7-1")); WebElement radio2 = driver.findElement(By.id("vfb-7-2")); System.out.println("Radio Button Option 1 Selected"); System.out.println("Radio Button Option 2 Selected"); WebElement option1 = driver.findElement(By.id("vfb-6-0")); if (option1.isSelected()) { System.out.println("Checkbox is Toggled On"); } else { System.out.println("Checkbox is Toggled Off"); } WebElement chkFBPersist = driver.findElement(By.id("persist_box")); for (int i=0; i<2; i++) { System.out.println("Facebook Persists Checkbox Status is - "+chkFBPersist.isSelected()); } } } TroubleshootingIf you encounter NoSuchElementException() while finding elements, it means that the element is not found in the page at the point the Web driver accessed the page.
Check your locator again using Firepath or Inspect Element in Chrome.
Check whether the value you used in the code is different from the one for the element in Firepath now.
Some properties are dynamic for few elements. In case, you find that the value is different and is changing dynamically, consider using By.xpath() or By.cssSelector() which are more reliable but complex ways.
Sometimes, it could be a wait issue too i.e., the Web driver executed your code even before the page loaded completely, etc.
Add a wait before findElement() using implicit or explicit waits.
Summary
The table below summarizes the commands to access each type of element discussed above
Element Command Description
Check Box, Radio Button used to toggle the element on/off
You're reading How To Select Radio Button And Checkbox In Selenium
Update the detailed information about How To Select Radio Button And Checkbox In Selenium on the Dacquyenphaidep.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!