0

I'm trying to check checkbox but unable to do. Below are the alternates that i've tried till now :

  1. (driver.findElement(By.xpath("//input[@name='selectedUpgCtn']")).click();
  2. (driver.findElement(By.name("selectedUpgCtn")).click();
  3. JavascriptExecutor js1 = (JavascriptExecutor)driver;

    js1.executeScript("arguments[0].click();",driver.findElement(By.xpath("//input[@name='selectedUpgCtn']")));
    
  4. js.executeScript("document.getElementByName('selectedUpgCtn').click()");

  5. (driver.findElement(By.name("selectedUpgCtn")).sendKeys(Keys.RETURN);
  6. (driver.findElement(By.name("selectedUpgCtn")).sendKeys(Keys.ENTER);
  7. the above 2 ways i have tried using xpath as well.

I have written code to wait, to check if the element is enabled or not, to check if element is clickable or not..but still can't check it. I'm also not getting any issue like element not found etc. the script simply stops there.Below is the code of that checkbox:

<input name="selectedUpgCtn" class="floatLeft ctncheckbox_6784290495 "type="checkbox"value="6784290495"targetsrc="wirelessUpgradeEligibilityModule" targetevent="disableStartUpgrade">

Any help would be really appreciated...

11
  • <input name="selectedUpgCtn" class="floatLeft ctncheckbox_6784290495 " type="checkbox" value="6784290495" targetsrc="wirelessUpgradeEligibilityModule" targetevent="disableStartUpgrade">------ this is the code of that checkbox.... Commented Jul 6, 2018 at 17:47
  • Check the HTML code in BOTH states (checked and unchecked) to see if there's a difference. You might find a new attribute appear or one of the attributes (possibly Value) change. Commented Jul 6, 2018 at 18:05
  • I just checked the html code in both states but there's no change in its code...but there is a button present at the bottom of the page....this button gets enabled and disabled with checking and unchecking the checkbox... Commented Jul 6, 2018 at 18:53
  • Have you checked for the presence of an IFRAME as the answer below suggests? That's the only other thing I can think of as well. Commented Jul 6, 2018 at 18:57
  • The element is present inside an iFrame and i'm already switching my frame to that frame....also just to see if element is being found or not i'm printing one of the attributes of this checkbox element just before clicking it with below code :- System.out.println(driver.findElement(By.xpath("//input[@name='selectedUpgCtn']")).getAttribute("name")); and this print statement is giving me expected o/p Commented Jul 6, 2018 at 19:14

1 Answer 1

1

Possibly your element is in frame of iframe, that's why try to find frame/iframe and use this:

driver.switchTo().frame("frame selector"); // switch to frame 
// now you are able to ineract with all elements inside it
driver.findElement(By.xpath("//input[@name='selectedUpgCtn']")).click(); // locate your element
driver.switchTo().defaultContent(); // switch back to default content

You are not able to interact with elements inside iframe or frame without switching to it's content.

Also try to wait for element until it will be clickable:

WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='selectedUpgCtn']")));
element.click();

EDIT: Now you are able to locate the element, now you want to click on it, but it is not working. Try to use:

WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='selectedUpgCtn']")));
Actions action = new Actions(driver);
action.moveToElement(element).click().build().perform();

This will simulate an actions chains => move to element and then click on it.

Or:

JavascriptExecutor js = (JavascriptExecutor)driver;   
js.executeScript("arguments[0].click();",driver.findElement(By.xpath("//input[@name='selectedUpgCtn']")));

Also I have faced at some of my tests, that some elements not getting clicked at the first time, but if I click on them two times and add some pause between first click and second click, it works. Try this also out.

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

9 Comments

The element is present inside an iFrame and i'm already switching my frame to that frame....also just to see if element is being found or not i'm printing one of the attributes of this checkbox element just before clicking it with below code :- System.out.println(driver.findElement(By.xpath("//input[@name='selectedUpgCtn']")).getAttribute("name")); and this print statement is giving me expected o/p.
What means o/p?
i just now tried using the WebDriverWait code that you posted but its neither checking that checkbox nor is it giving any error.....
I don't understand you, did you get your issue solved or not?
by o/p i mean....the value of the attribute "name" which is "selectedUpgCtn".........this is getting printed on my console....that means my locator code is able to find the checkbox but somehow its not getting checked...after implementing your WebDriverWait code i think the click method is getting executed properly but there's something which is preventing my checkbox to get checked....
|

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.