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.
-
What's your question? Are you looking for the element's xpath? – GalAbra Jan 21 '18 at 20:46
-
even on using the elements xpath, it's opening a link. I want to click on label::before or get xpath for label::before – Ameet Pradhan Jan 21 '18 at 20:57
-
In the picture you're focusing the wrong element. I believe you're looking to click the element with id=`term1` – GalAbra Jan 21 '18 at 21:03
-
yeah the id is term1 but clicking on term1 opens 'Terms of Use'. – Ameet Pradhan Jan 21 '18 at 21:08
-
let me check this – Ameet Pradhan Jan 21 '18 at 21:18
-
WebElement Checkbox = wd.findElement(By.xpath("//label[@for='term1']")); Actions myMouse = new Actions(wd); myMouse.moveToElement(Checkbox, -180, 0).click().build().perform(); This code worked. Thanks a lot – Ameet Pradhan Jan 25 '18 at 16:51
2 Answers
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();

- 22,180
- 5
- 32
- 55
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();
}
}

- 831
- 1
- 11
- 23