Anand Prasath
Anand Prasath

Reputation: 1

Selenium Click is not happening for checkbox within label element

I am trying to click a checkbox on my webpage and the DOM is given below:

<label class='checkbox-module-container'>
<input type='checkbox' class='dummyclass'>

When I tried to click on the input checkbox, I am getting 'Element not clickable' exception.

Tried with JavaScript executor also but no luck.

Is there any workaround to make element click on those elements which are overlapped?

Upvotes: 0

Views: 1156

Answers (1)

cruisepandey
cruisepandey

Reputation: 29362

If the HTML DOM is

<label class='checkbox-module-container'>
    <input type='checkbox' class='dummyclass'>
    </input>
</label>

then you can try to click on it like below.

There are 4 ways to click in Selenium.

I will use this xpath

//label[contains(@class,'checkbox-module-container')]//child::input[@type='checkbox']

Code trial 1 :

Thread.sleep(5);
driver.findElement(By.xpath("//label[contains(@class,'checkbox-module-container')]//child::input[@type='checkbox']")).click();

Code trial 2 :

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[contains(@class,'checkbox-module-container')]//child::input[@type='checkbox']"))).click();

Code trial 3 :

Thread.sleep(5);
WebElement button = driver.findElement(By.xpath("//label[contains(@class,'checkbox-module-container')]//child::input[@type='checkbox']"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", button);

Code trial 4 :

Thread.sleep(5);
WebElement button  = driver.findElement(By.xpath("//label[contains(@class,'checkbox-module-container')]//child::input[@type='checkbox']"));
new Actions(driver).moveToElement(button).click().build().perform();

PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

Upvotes: 2

Related Questions