简体   繁体   中英

Can't Select Checkbox, Selenium WebDriver

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

在此处输入图片说明

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.

Try using JavascriptExecuter Hope this will help

WebElement element = driver.findElement(By.id("Terms1"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", element );

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 as termsCheck`. 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.

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM