How to handle a checkbox on a webpage and check its state using Selenium Webdriver?

0 votes
How to handle a checkbox on a webpage and check its state using Selenium Webdriver?
May 14, 2019 in Selenium by Chris
704 views

1 answer to this question.

0 votes

Hey Chris, to handle a checkbox and check its status using Selenium Webdriver, follow these steps:

1. Launch the Eclipse IDE and right click on the src folder and then click on the New > class. Enter the class name as Checkbox_test:

Handling Checkbox

 

2. Now, invoke the Google Chrome browser by setting the system property to the path of your chromedriver.exe file:

System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe);
WebDriver driver = new ChromeDriver(); 

3. Next, navigate to the SpiceJet website:

driver.navigate().to("https://www.spicejet.com/");  

4. Then try to locate the 'Senior Citizen' checkbox by inspecting its HTML code:

5. Write the code that will locate 'Senior Citizen' checkbox and check whether its checked or not by using these line of code:

System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).click();  
System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());

6. Finally, click on Run button to execute the automation. You would see this result in your Console:

answered May 14, 2019 by Anvi
• 14,050 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer

How to check all checkboxes on a webpage using Selenium?

Hey Jenny, to check all the checkboxes ...READ MORE

answered Jun 25, 2019 in Selenium by Suraj
164 views
0 votes
1 answer

How to get all options of a dropdown on a webpage using Selenium Webdriver?

Hey Joel, you can use following lines ...READ MORE

answered Jul 15, 2019 in Selenium by Abha
• 27,920 points
397 views
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,510 points
2,636 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,870 points
2,070 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,870 points
2,183 views
0 votes
1 answer

How to automate radio button and checkbox on a webpage in Selenium Webdriver?

Hey Yashmita, following code snippet automates the ...READ MORE

answered Jul 22, 2019 in Selenium by Anvi
• 14,050 points
689 views
0 votes
1 answer

How to handle checkboxes on a webpage using Ruby Selenium Webdriver?

Hi Sandeep, you can use following code ...READ MORE

answered Aug 26, 2019 in Selenium by Anvi
• 14,050 points
190 views