0

How can I write in Selenium with java the code to uncheck one of these checkboxes? In the original search are displayed checked but I want to uncheck one or two or all three to see less results, this is the displayed search:

Filter results by provider: [x]Facebook (25911), [x]Hotmail (7651), [x]Yahoo (11)

Thank you for your help :)

3
  • BTW, this is the 2 ways I am trying but none is working: driver.findElement(By.xpath("//input[@id='custom-checkbox-6']")).click(); driver.findElement(By.cssSelector("input[id='custom-checkbox-6']")).click(); Commented May 10, 2018 at 9:12
  • Add the relevant html also Commented May 10, 2018 at 9:36
  • Get all elements which you wish in collection. Iterate over it, and then by specific use case, check/uncheck what you wish(with parameters). driver.findElements() will help you. I think you must separate this on steps e.g, get all target elements, get desired(by check/uncheck), assert state. Commented May 10, 2018 at 11:58

4 Answers 4

1

A colleague helped me and it was very easy, here is the way it worked:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("custom-checkbox[label^='Yahoo']"))).click();

Thank you for your help. :)

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

Comments

1

I see this is an old question but I recently ran into this myself. I did not need to know ahead of time if the box was checked, I just wanted to force it unchecked. The solution I came up with (using Javascript in Node.js) is similar to what I use to call setAttribute:

let element = await driver.findElement(By.id("checkBox1"));
if (element != null) {
  await driver.executeScript("arguments[0].checked = false;", element);
}

This forces the box to be unchecked regardless of whether or not it was first checked. No clicks required.

Comments

0

Some elements can be really fussy.

Try using .sendKeys(Keys.Return) rather than .click().

Comments

0

Get the list of filter result in List (list of Facebook (25911) , Hotmail(7651) and yahoo, Let say list B).

Now iterate for loop on that list of webelements(B) and if that element is the same that you want to uncheck then you can have a index of that element(Let say you want to uncheck Hotmail so you will get index 1) now just using that index get a element using that index in xpath (Ex:- //ul[@ag-id='gallery1']/li["(+i+1)+"]/div/div/h4/a). and click on that element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.