How can I select checkboxes using the Selenium Java WebDriver?
Solution 1
Selecting a checkbox is similar to clicking a button.
driver.findElement(By.id("idOfTheElement")).click();
will do.
However, you can also see whether the checkbox is already checked. The following snippet checks whether the checkbox is selected or not. If it is not selected, then it selects.
if ( !driver.findElement(By.id("idOfTheElement")).isSelected() )
{
driver.findElement(By.id("idOfTheElement")).click();
}
Solution 2
It appears that the Internet Explorer driver does not interact with everything in the same way the other drivers do and checkboxes is one of those cases.
The trick with checkboxes is to send the Space key instead of using a click (only needed on Internet Explorer), like so in C#:
if (driver.Capabilities.BrowserName.Equals(“internet explorer"))
driver.findElement(By.id("idOfTheElement").SendKeys(Keys.Space);
else
driver.findElement(By.id("idOfTheElement").Click();
Solution 3
If you want to click on all checkboxes at once, a method like this will do:
private void ClickAllCheckboxes()
{
foreach (IWebElement e in driver.FindElements(By.xpath("//input[@type='checkbox']")))
{
if(!e.Selected)
e.Click();
}
}
Solution 4
Solution for C#
try
{
IWebElement TargetElement = driver.FindElement(By.XPath(xPathVal));
if (!TargetElement.Selected)
{
TargetElement.SendKeys(Keys.Space);
}
}
catch (Exception e)
{
}
Solution 5
You can use the following code:
List<WebElement> checkbox = driver.findElements(By.name("vehicle"));
((WebElement) checkbox.get(0)).click();
My HTML code was as follows:
<.input type="checkbox" name="vehicle" value="Bike">I have a bike<br/>
<.input type="checkbox" name="vehicle" value="Car">I have a car<br/>
Related videos on Youtube
Maximus
Updated on July 09, 2022Comments
-
Maximus almost 2 years
How can I check the checkboxes using an id or XPath expression? Is there a method similar to select by visibletext for a dropdown?
Going through the examples given for all other related questions, I could not find a proper solution that works in a concise way that by few line or method I can check a chekbox or radio button.
A sample HTML section is below:
<tbody> <tr> <td> <span class="120927"> <input id="ctl00_CM_ctl01_chkOptions_0" type="checkbox" name="ctl00$CM$ctl01$chkOptions$0"/> <label for="ctl00_CM_ctl01_chkOptions_0">housingmoves</label> </span> </td> </tr> <tr> <td> <span class="120928"> <input id="ctl00_CM_ctl01_chkOptions_1" type="checkbox" name="ctl00$CM$ctl01$chkOptions$1"/> <label for="ctl00_CM_ctl01_chkOptions_1">Seaside & Country Homes</label> </span> </td> </tr> </tbody>
-
Arran over 11 yearsWell what does your HTML look like? It's impossible to answer without looking at a sample, even a very brief sample, of what your page looks like.
-
Josh Correia almost 4 yearsDoes this answer your question? Click Check-box from the list of Check boxes via Selenium/Webdriver
-
-
Maximus over 11 yearsWell I am able to select dropdown values using below code and want some similar option to select checkboxes and radio button. driver.findElement(By.xpath("html/body/form/div[5]/div[3]/div[1]/div[2]/table[1]/tbody/tr[1]/td/div[2]/div[2]/ul/li/select")).click(); Select option = new Select(driver.findElement(By.id("ctl00_CM_ctl00_ddlOptions"))); option.selectByVisibleText("Yes");
-
Maximus over 11 years@Arran Below is the extract of my HTML. '<tbody> <tr> <td> <span class="120927"> <input id="ctl00_CM_ctl01_chkOptions_0" type="checkbox" name="ctl00$CM$ctl01$chkOptions$0"/> <label for="ctl00_CM_ctl01_chkOptions_0">housingmoves</label> </span> </td> </tr> <tr> <td> <span class="120928"> <input id="ctl00_CM_ctl01_chkOptions_1" type="checkbox" name="ctl00$CM$ctl01$chkOptions$1"/> <label for="ctl00_CM_ctl01_chkOptions_1">Seaside & Country Homes</label> </span> </td> </tr> </tbody>'
-
Maximus over 11 yearsSorry guys! I m new to this and do not know how differentiate code, URL and comment.
-
Maximus over 11 years@ Code Enthusaiastic <code> 'driver.findElement(By.id("idOfTheElement").click();' </code> It is not working in my case. Please see my HTML code. I am using IE8 driver.
-
Arran over 11 yearsWorks fine for me here, with your HTML. Therefore there is something else you aren't telling us. How is it not working? Is it finding anything?
-
Code Enthusiastic over 11 yearsI was just trying to say how to select a check box. If the attribute id of an element is dynamic then you may need to rely on css or xpath.
-
Bob over 10 yearsSendKeys also is the solution for the C# driver. Thanks.
-
dmeehan over 9 yearsI found I had to use the send a space key method with ChromeDriver also. Ive tested the send space key method with Chrome Drive v2.14 and IEDriver 2.44.
-
Arnab over 8 yearsthe above solution is perfect.If you want to add an extra checking if the check box is already checked or not the u can do the same like below. try{ IWebElement TargetElement = driver.FindElement(By.XPath(xPathVal)); if (!TargetElement.Selected) { TargetElement.SendKeys(Keys.Space); } } catch (Exception e) { }
-
codepleb about 6 yearsTypecast in here seems obsolete.
-
Peter Mortensen over 3 yearsIn Python it is
.send_keys(Keys.SPACE)
instead of.SendKeys(Keys.Space)
(3 differences).Keys
requiresfrom selenium.webdriver.common.keys import Keys
. -
Peter Mortensen over 3 yearsHow is it different from previous answers?
-
Peter Mortensen over 3 yearsYes, this seems like a more robust solution, not making assumptions about the current state of the checkboxes.
-
Peter Mortensen over 3 yearsYes, this seems like a more robust solution, not making assumptions about the current state of the checkboxes.
-
Peter Mortensen over 3 yearsYes, this seems like a more robust solution, not making assumptions about the current state of the checkboxes.
-
Peter Mortensen over 3 yearsIn Python, it is
.is_selected()
(two changes). -
Peter Mortensen over 3 yearsIn Python, it is
.is_selected()
(two changes). -
Peter Mortensen over 3 yearsIn Python, it is
.is_selected()
(two changes). -
Peter Mortensen over 3 yearsHow is it different from Scott Crowe's answer and other answers?
-
Peter Mortensen over 3 yearsIn Python it is
.send_keys(Keys.SPACE)
instead of.SendKeys(Keys.Space)
(three differences).Keys
requiresfrom selenium.webdriver.common.keys import Keys
. -
Peter Mortensen over 3 yearsIn Python it is
.send_keys(Keys.SPACE)
instead of.SendKeys(Keys.Space)
(three differences).Keys
requiresfrom selenium.webdriver.common.keys import Keys
. -
Peter Mortensen over 3 yearsIn Python it is
.send_keys(Keys.SPACE)
instead of.SendKeys(Keys.Space)
(three differences).Keys
requiresfrom selenium.webdriver.common.keys import Keys
. -
Peter Mortensen over 3 yearsWhat language? The use of
.Click()
(uppercase "c") suggests it is C# (like Faiz's answer). All other bindings, including Python, use lowercase (.click()
). -
Peter Mortensen over 3 yearsWhat language? The use of
.Click()
(uppercase "c") suggests it is C# (like Faiz's answer). All other bindings, including Python, use lowercase (.click()
). -
Peter Mortensen over 3 yearsIn Python, it is
.is_selected()
(three changes) and.click()
(lowercase), respectively. -
Peter Mortensen over 3 yearsIn Python it is
.click()
(lowercase). -
Peter Mortensen over 3 years
-
Peter Mortensen over 3 yearsIn Python, it is
.is_selected()
(three changes). -
Peter Mortensen over 3 years
-
Peter Mortensen over 3 yearsWhat language is this? Java? (The question specified Java, but at least 3 answers have used C#.)