0

How do I click on checkbox using selenium web driver in Java?

Code is:

<div class="icheckbox_square-purple" aria-checked="false" 
  aria-disabled="false" style="position: relative;">
<input type="checkbox" class="check initialChk" value="7" 
  id="7" style="position: absolute; opacity: 0;">
<ins class="iCheck-helper" 
  style="position: absolute; 
    top: 0%; left: 0%; display: block; 
    width: 100%; height: 100%; 
    margin: 0px; padding: 0px; 
    border: 0px; opacity: 0; 
    background: rgb(255, 255, 255);">
</ins>
</div>
John Hascall
  • 9,176
  • 6
  • 48
  • 72
kunal soni
  • 585
  • 1
  • 7
  • 19
  • 2
    Does this answer your question? [Click Check-box from the list of Check boxes via Selenium/Webdriver](https://stackoverflow.com/questions/11888786/click-check-box-from-the-list-of-check-boxes-via-selenium-webdriver) – Josh Correia Sep 04 '20 at 19:28

6 Answers6

0

First you have to find this element, for example by xPath, and then do click() action on this element.

Possible duplicate: How to select checkboxes using selenium java webdriver?

Community
  • 1
  • 1
DarSta
  • 155
  • 11
0
WebElement chk = driver.findElement(By.className("initialChk"));     
chk.click();
Sanchita
  • 84
  • 3
  • what is the error? Do you have multiple check-boxes with the same ID or class name on the page? If yes, use findElements method instead of findElement. – Sanchita Feb 25 '16 at 13:35
0

In the first step you have to locate your checkbox by some locator, here I'm using id as an element locator.

By ChkName = By.id("7"); WebElement chkBox = driver.findElement(ChkName).click();

That's all it's simple!

0
WebElement box = driver.FindElement(By.className("initialChk"));
    {
        if(!box.Selected)
            box.Click();
    }
karthick23
  • 1,313
  • 1
  • 8
  • 15
0

Duplicate issue. You can try this :

driver.findElement(By.id("idOfTheElement")).click();
Diptman
  • 374
  • 3
  • 14
0

Apart from other answers, you can use this;

your_web_driver.findElement(By.xpath("//input[@type='checkbox']")).click();

to check every check box in a page.

Y.Kaan Yılmaz
  • 612
  • 5
  • 18