0

I've been puzzling at this for a few hours now. Checked through similar issues on Stackoverflow but been unable to find a solution.

I have a checkbox I'm trying to use Selenium webdriver to check, but when I run the script, I don't get any error messages but the checkbox remains unchecked.

I'm using the line below to select and check the box

IWebElement checkBox = m_driver.FindElement(By.XPath("//div[2]/label/span"));
checkBox.Click();

I've copied the HTML from it below.

<div class="input-group single-option label-empty" >
    <label class="" >
        <input type="checkbox" name="privacy" value="true"  required  />
            <span>I have read and understood the <a data-toggle="#privacy-terms" 
                  data-group="privacy-terms">Privacy Policy</a> and <a data-toggle=
                  "#terms" data-group="privacy-terms">Terms and Conditions</a>. </span>
    </label>

I'd really be grateful for some help. I'm pretty new to automation...and C#

Unfortunately, I am unable to post the url as it's a passworded client site. If I can post the HTML, that may help...

I've posted the html from the page in question but removed the client name :)

Link to HTML

If I select the checkbox through Chrome developer tools and copy the Xpath, I get this.

IWebElement checkBox = m_driver.FindElement(By.XPath("/html/body/div[1]/main/section/form/div[2]/label/input"));

I re-ran it and Selenium generated an error.

Message: OpenQA.Selenium.WebDriverException : unknown error: Element is not clickable at point (491, 593). Other element would receive the click: ... (Session info: chrome=71.0.3578.98) (Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 10.0.17134 x86_64)

developer tools

Here is a screenshot of developer tools with the checkbox ticked. Could it be something to do with the 'span'?

6
  • this XPATH: "//div[2]/label/span" is not a robust one... can't you find a better one? Commented Dec 20, 2018 at 17:04
  • also it will help if you can add the url if you can... Commented Dec 20, 2018 at 17:04
  • "//input[@type='checkbox'][@name='privacy']" try this xpath Commented Dec 20, 2018 at 17:20
  • And btw, try to avoid absolute xpath, use it when you do not have any other option. Commented Dec 20, 2018 at 17:23
  • Unfortunately that didn't work. The error was generated again. Commented Dec 20, 2018 at 17:25

2 Answers 2

2

I figured it out. Instead of using "checkBox.Click();", I used "checkBox.SendKeys(Keys.Space);

and it works. Now I'm trying to do the same with the recaptcha which by definition should be difficult.

Sign up to request clarification or add additional context in comments.

Comments

1

You are clicking the SPAN not the INPUT that is the checkbox. This should work.

m_driver.FindElement(By.CssSelector("input[name='privacy']")).Click();

You may need a wait, depending on what is going on before the click.

Your error about element is not clickable could be any number of things. It could be popup blocking it, a floating DIV panel, a loading spinner, ... etc. You will need to deal with the popup (by closing it, etc.), floating DIV may require the page to be scrolled, or for the spinner a wait for the spinner to become invisible. It's hard to say without more information.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.