4

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?

SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
user2359634
  • 1,275
  • 5
  • 14
  • 27

3 Answers3

3

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()

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

I tried it the same way as you, only in C#. I was able to click the CheckBox and saw it change its state on the screen. But when I tried to read the selected state of the checkbox, I got an exception: stale element reference: element is not attached to the page document.

If I use the page factory to obtain the WebElement, this doesn't happen. So maybe you should try Selenium's PageFactory pattern. It's cleaner anyway.

Kim Homann
  • 3,042
  • 1
  • 17
  • 20
0

There are a couple of ways to handle it , please see below :

  1. Using action class for the click : Link to Official documentation

    As the method documentation says,

Call perform() at the end of the method chain to actually perform the actions.

The general way to achieve click using Actions class is below :

actionsObj.moveToElement(element1).click().build().perform()
  1. If Actions class fails , sometimes the reason can be that you receive below exception :

    ElementNotInteractableException [object HTMLSpanElement] has no size and location

That can mean two things :

a. Element has not properly rendered: Solution for this is just to use implicit /explicit wait

  • Implicit wait :

    driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

  • Explicit wait :

    WebDriverWait wait=new WebDriverWait(driver, 20); element1 = wait.until(ExpectedConditions.elementToBeClickable(By.className("fa-stack-1x")));

b. Element has rendered but it is not in the visible part of the screen: Solution is just to scroll till the element. Based on the version of Selenium it can be handled in different ways but I will provide a solution that works in all versions :

    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("arguments[0].scrollIntoView(true);", element1);
  1. Suppose all this fails then another way is to again make use of Javascript executor as following : executor.executeScript("arguments[0].click();", element1);
user3251882
  • 922
  • 1
  • 11
  • 21