How can I select checkboxes using the Selenium Java WebDriver?

266,818

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/>
Share:
266,818

Related videos on Youtube

Maximus
Author by

Maximus

Updated on July 09, 2022

Comments

  • Maximus
    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>
    
  • Maximus
    Maximus over 11 years
    Well 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]/di‌​v[1]/div[2]/table[1]‌​/tbody/tr[1]/td/div[‌​2]/div[2]/ul/li/sele‌​ct")).click(); Select option = new Select(driver.findElement(By.id("ctl00_CM_ctl00_ddlOptions")‌​)); option.selectByVisibleText("Yes");
  • Maximus
    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
    Maximus over 11 years
    Sorry guys! I m new to this and do not know how differentiate code, URL and comment.
  • Maximus
    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
    Arran over 11 years
    Works 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
    Code Enthusiastic over 11 years
    I 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
    Bob over 10 years
    SendKeys also is the solution for the C# driver. Thanks.
  • dmeehan
    dmeehan over 9 years
    I 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
    Arnab over 8 years
    the 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
    codepleb about 6 years
    Typecast in here seems obsolete.
  • Peter Mortensen
    Peter Mortensen over 3 years
    In Python it is .send_keys(Keys.SPACE) instead of .SendKeys(Keys.Space) (3 differences). Keys requires from selenium.webdriver.common.keys import Keys.
  • Peter Mortensen
    Peter Mortensen over 3 years
    How is it different from previous answers?
  • Peter Mortensen
    Peter Mortensen over 3 years
    Yes, this seems like a more robust solution, not making assumptions about the current state of the checkboxes.
  • Peter Mortensen
    Peter Mortensen over 3 years
    Yes, this seems like a more robust solution, not making assumptions about the current state of the checkboxes.
  • Peter Mortensen
    Peter Mortensen over 3 years
    Yes, this seems like a more robust solution, not making assumptions about the current state of the checkboxes.
  • Peter Mortensen
    Peter Mortensen over 3 years
    In Python, it is .is_selected() (two changes).
  • Peter Mortensen
    Peter Mortensen over 3 years
    In Python, it is .is_selected() (two changes).
  • Peter Mortensen
    Peter Mortensen over 3 years
    In Python, it is .is_selected() (two changes).
  • Peter Mortensen
    Peter Mortensen over 3 years
    How is it different from Scott Crowe's answer and other answers?
  • Peter Mortensen
    Peter Mortensen over 3 years
    In Python it is .send_keys(Keys.SPACE) instead of .SendKeys(Keys.Space) (three differences). Keys requires from selenium.webdriver.common.keys import Keys.
  • Peter Mortensen
    Peter Mortensen over 3 years
    In Python it is .send_keys(Keys.SPACE) instead of .SendKeys(Keys.Space) (three differences). Keys requires from selenium.webdriver.common.keys import Keys.
  • Peter Mortensen
    Peter Mortensen over 3 years
    In Python it is .send_keys(Keys.SPACE) instead of .SendKeys(Keys.Space) (three differences). Keys requires from selenium.webdriver.common.keys import Keys.
  • Peter Mortensen
    Peter Mortensen over 3 years
    What 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
    Peter Mortensen over 3 years
    What 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
    Peter Mortensen over 3 years
    In Python, it is .is_selected() (three changes) and .click() (lowercase), respectively.
  • Peter Mortensen
    Peter Mortensen over 3 years
    In Python it is .click() (lowercase).
  • Peter Mortensen
    Peter Mortensen over 3 years
    What language? The use of .Click() (uppercase "c") suggests it is C# (both Java and Python ruled out). All other bindings, including Python, use lowercase (.click()).
  • Peter Mortensen
    Peter Mortensen over 3 years
    In Python, it is .is_selected() (three changes).
  • Peter Mortensen
    Peter Mortensen over 3 years
    What language in the last part? The use of .Click() (uppercase "c") suggests it is C# (both Java and Python ruled out). All other bindings, including Python, use lowercase (.click()).
  • Peter Mortensen
    Peter Mortensen over 3 years
    What language is this? Java? (The question specified Java, but at least 3 answers have used C#.)