Sam M
Sam M

Reputation: 55

How to Click on a Checkbox Selenium

My goal is to check a checkbox on a website. The html:

<label class="checkbox" for="are_terms_agreed">
<input name="terms" id="are_terms_agreed" class="checkbox__input required-entry validate-checkbox" value="1" type="checkbox">
<span class="checkbox__span">
<i class="checkbox__icon"></i>
</span>
<span class="checkbox__title">
I have read and agree to the 
<span class="signup__label-terms">
<a onclick="return false;" href="https://shop.adidas.ae/en/help/terms-conditions.html" class="js-dialog-btn" data-target="js-dialog-terms">
Terms &amp; Conditions
</a>
for website use
</span>
</span>
</label>

Original link: https://shop.adidas.ae/en/checkout/onepage/ (you might have to add a shoe to a cart and proceed the checkout process to view the page I'm viewing)

I have tried a few different ways to accomplish this. Firstly, I have tried to access the

IWebElement termsandconditions2 = driver.FindElement(By.XPath("//label[@class = 'checkbox']//label[@for = 'are_terms_agreed']"));

I get the error:

An unhandled exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll Additional information: Unable to locate element: {"method":"xpath","selector":"//label[@class = 'checkbox']//label[@for = 'are_terms_agreed']"}

I have also tried:

IWebElement termsandconditions = driver.FindElement(By.CssSelector("span[class*='checkbox__span']"));

This is basically clicking on the actual "check" icon, rather than checking the whole box as I tried before. When I run this, I get the error:

An unhandled exception of type 'OpenQA.Selenium.ElementNotVisibleException' occurred in WebDriver.dll Additional information: Element is not currently visible and so may not be interacted with

I have tried using scroll down code, nothing happens.

Any help? Thanks

Upvotes: 0

Views: 21701

Answers (1)

Turgut Kanceltik
Turgut Kanceltik

Reputation: 639

According to the coordinates click event. Try it.

Actions action = new Actions(driver);
action.MoveToElement(driver.FindElement(By.Xpath("//*[@id='co-terms-form']/label[1]/span[1]")), 2, 2).Click().Perform();

Or:

driver.FindElement(By.Xpath("//*[@id='co-terms-form']/label[1]/span[1]").CLick()

Upvotes: 2

Related Questions