0

I'm trying to test the privacy statement checkbox on this website

enter image description here

The HTML code of the checkbox is

<input type="checkbox" name="registryTandCs" id="registryTandCs" value="" class="form-check-input form-check-input--custom" required="required" data-parsley-multiple="registryTandCs">

The code that I am using to check this is

driver.findElement(By.id("registryTandCs")).click();

However, it doesn't seem to work. I have also tried

WebElement checkBox = driver.findElement(By.id("registryTandCs"));
checkBox.click();

If it's any help, the website in question is here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

1

The main problem is that when you are trying to click the checkbox other element is getting clicked. You can see an input and label tags are getting overlapped (Please check the attached image).

Screenshot:

enter image description here

Always check the error stacktrace.

org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element 
<input type="checkbox" name="registryTandCs" id="registryTandCs" value="" class="form-check-input form-check-input--custom" required="required" data-parsley-multiple="registryTandCs"> 
is not clickable at point (655, 517). 
Other element would receive the click: 
<label for="registryTandCs" class="form-check-label form-check-label--grey">...</label>

Working solution: There is two way to solve this problem.

Using Action Class:

WebElement checkBox = driver.findElement(By.id("registryTandCs"));
Actions action = new Actions(driver);
action.moveToElement(checkBox).click().build().perform();

Using JavascriptExecutor:

WebElement checkBox = driver.findElement(By.id("registryTandCs"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", checkBox);
Abhishek Dhoundiyal
  • 1,359
  • 1
  • 12
  • 19
0

To click() on the element you can use either of the following Locator Strategies:

  • id:

    driver.findElement(By.id("registryTandCs")).click();
    
  • name:

    driver.findElement(By.name("registryTandCs")).click();
    
  • cssSelector:

    driver.findElement(By.cssSelector("input#registryTandCs[name='registryTandCs']")).click();
    
  • xpath:

    driver.findElement(By.xpath("//input[@id='registryTandCs' and @name='registryTandCs']")).click();
    

Ideally to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • id:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("registryTandCs"))).click();
    
  • name:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.name("registryTandCs"))).click();
    
  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#registryTandCs[name='registryTandCs']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='registryTandCs' and @name='registryTandCs']"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352