0

I am unable to select a checkbox with Selenium WebDriver in Java. I tried by Xpath but no result. WebDriver can't click on element. I tried with Selenium IDE - recorder, no results.

Here it is - html code for checkbox

enter image description here

I try:

1.

driver.findElement(By.xpath(".//form[@id='placeOrderForm1']/div[@class='terms right']/label")).click();

2.

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

3.

driver.findElement(By.cssSelector("label")).click();

4.

driver.findElement(By.xpath("//div[3]/form/div/input")).click();

Nothing works. Please help.

5
  • At a first glance it seems that you are clicking the label, at least in cases 1 and 3. Commented Jun 8, 2017 at 12:02
  • May be this page inside the frame. please check the page contains any iframe tag. if this checkbox is inside frame, then use switch to frame method and try find element. Commented Jun 8, 2017 at 12:04
  • And are you getting an error message? Commented Jun 8, 2017 at 12:07
  • Just give some wait time and use driver.findElement(By.id("Terms1")).click(); Commented Jun 8, 2017 at 12:18
  • Click it manually and check what has changed in html) when you click on checkbox by id - it just do nothing or you get an exception? which one? Commented Jun 8, 2017 at 13:04

4 Answers 4

2

Try using JavascriptExecuter Hope this will help

WebElement element = driver.findElement(By.id("Terms1"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", element );
Sign up to request clarification or add additional context in comments.

1 Comment

Hi. I get - org.openqa.selenium.WebDriverException: TypeError: can't access dead object I'm stuck here :/
1

Your code seems correct. Specially this one -

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

It might be possible the element you are clicking is not visible in the page scroll. Try to move to the element first and then click.

Try with this -

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

Hope this help.

3 Comments

Try driver.switchTo().defaultContent(); before using findElement. Let me know if that works.
This issue generally occurs when focus is not on the frame on which you are finding the element. If the Element on which you are performing operation is on different frame then switch to that frame first before finding element.
Yes! FINALLY.The problem is that he left out of his default frame.
0

Here is the Answer of your Question:

As you mentioned unable to select a checkbox, actually we don't select the checkbox, we checkmark the checkbox. The checkbox you depicted have an id as Terms1 and name astermsCheck`. So you use either of the locators to checkmark the checkbox as follows:

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

OR

element = driver.findElement(By.name("termsCheck")).click();

Let me know if this Answers your Question.

2 Comments

Hi.I try but I got ---- org.openqa.selenium.WebDriverException: TypeError: can't access dead object
@Machete Can you consider to showcase your work please? Thanks
0

You can find the element by a unique identifier. In this case, we can use name or id. The better choice is to go with id.

WebElement element = driver.findElement(By.name("termsCheck"));
element.click();

or you can use this one also

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

2 Comments

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
Sure, I'll add. Thanks for your advice.

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.