-1

Checkbox-to be automated

Above is a picture of the check-box to be automated which on inspection shows element name as label::before. Using id or name simply opens some other link. Please help regarding selecting the checkbox using Selenium - Java.

Ameet Pradhan
  • 23
  • 1
  • 3
  • 10

2 Answers2

1

You are attempting to click the wrong element. The element you want to click is the INPUT right above.

<input name="term1" id="term1" type="checkbox">

You can do this simply by using the id, term1.

driver.findElement(By.id("term1")).click();
JeffC
  • 22,180
  • 5
  • 32
  • 55
0

My experience with checkboxes is that you have to click the label next to the checkboxes to set them. Clicking on the input will result in an OpenQA.Selenium.ElementNotVisibleException exception. You can get the status of the checkbox by Element.Selected (C#) or Element.isSelected(Java) but you can not interact with it like Clicking on it.

You can find the label by XPath using the text "//label[contains(text(),'Agree to our')]" or using the attribute for "//label[contains(@for,'term1')]"

The problem that the "Terms of Use" link opens can be avoided by using the Action Class to position the click exactly in the upper left corner of the label by using xoffset = 1 and yoffset = 1. This exact positioning is necessary because in the HTML the anchor tags are nested within the label tag. I tried this code on your website, it was giving the correct result.

In C#, using PageObject/PageFactory the solution will be:

public class TestPage
{
    private readonly IWebDriver _driver;

    public  TestPage(IWebDriver driver)
    {
        _driver = driver;
        PageFactory.InitElements(driver, this);
    }

    [FindsBy(How = How.XPath, Using = "//label[contains(text(),'Agree to our')]")]
    private IWebElement _labelInputCheckbox;

    public void ClickLabelInputCheckbox()
    {
        var action = new OpenQA.Selenium.Interactions.Actions(_driver);
        action.MoveToElement(_labelInputCheckbox,1,1).Click();
        action.Perform();
    }
}
Frank
  • 831
  • 1
  • 11
  • 23