Click checkbox not working using Selenium webdriver even if checkbox is displayed

12,452

You may try moving to the checkbox and then clicking:

Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();

Or, make the click via JavaScript:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);

But, make sure you understand the reason you have to do it: WebDriver click() vs JavaScript click()

Share:
12,452
user2359634
Author by

user2359634

Technology enthusiast with realistic and practical approach to implement workable solutions. 5 years and 3 months of development experience on Java/J2EE technology stack. Experience of working on large scale products. Hands on experience on Java, Spring, Hibernate, MySQL, RESTful Web services. Experience in designing application using object oriented approach and design patterns. Experience of working with Scrum Agile methodology. Ensuring the development to meet quality standards by choosing right tools and technologies, creating prototypes and implementing best practices. Communicating with clients to understand project requirement and propose the best solution. Cohesive team worker, having strong analytical and problem solving skills.

Updated on June 25, 2022

Comments

  • user2359634
    user2359634 about 2 years

    Using Selenium webdriver, I am trying to click checkbox but not able to do so, even if the element is displayed.

    Below is my code:

    WebElement element = 
    new WebDriverWait(webDriver, 1000).until(ExpectedConditions.presenceOfElementLocated(By.xpath(prop.getProperty(object))));
    element.isDisplayed(); // returns true
    element.getAttribute("type"); // returns checkbox
    element.isSelected(); // returns false
    
    element.click();
    element.isSelected(); // still returns false
    

    The element is displayed, but I am still not able to select the checkbox. What could be the reason? Where am I going wrong?