1

I am trying to check whether a checkbox is selected. My checkbox is styled using input and span, not using the checkbox tag. As it's not a default checkbox I can't use methods such as isSelected or isChecked to check its state. I was then trying to check if any class belongs to a state but not the other. However, the only difference I've found so far is that when the element is selected an ::after appears but not sure how to go about checking this?

I found a tutorial with a similar issue, but don't know much about Javascript and not sure how to adapt it to my case.

https://www.quora.com/How-do-I-locate-After-and-Before-css-tag-in-selenium-webdriver

Before clicked enter image description here

After clicked enter image description here

That's what is being used and as per @pguardiario answer

 System.out.println(js.executeScript("return window.getComputedStyle(document.querySelector('.custom-checkmark'), ':after').getPropertyValue('content')"));

But both when it's selected or not it returns the same output (empty string)

UPDATE

Found the difference between the selected and unselected states. The .custom-checkmark:after style has display-none when the checkbox is not selected. Not sure still how to use this info as that's what I have at moment and they return display none both before and after the checkbox is clicked.

@Test
public void testingCheckbox() {

    JavascriptExecutor js = (JavascriptExecutor) wd;

    System.out.println(js.executeScript("return window.getComputedStyle(document.querySelector('.custom-checkmark'), ':after').getPropertyValue('display')"));

    lp.clickCheckBox();

    System.out.println(js.executeScript("return window.getComputedStyle(document.querySelector('.custom-checkmark'), ':after').getPropertyValue('display')")); 
}

enter image description here

NEW FINDING

It seems there's actually 'two checkboxes'. One with the span tag and the other one with the span. They appear together when unselecting some attributes.

enter image description here

Thanks for the help.

Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81

4 Answers4

1

Try the below CSS selectors for identifying that

span.custom-checkmark:after

And also please see the below link for more details Extracting content in :after using XPath

1

I can't test it but it should look something like this:

driver.executeScript('return window.getComputedStyle(document.querySelector(".custom-checkmark"), ":after").getPropertyValue("content")')

Sorry about the long line btw, Java doesn't have heredocs which makes this painful :(

pguardiario
  • 53,827
  • 19
  • 119
  • 159
1

Try use JavascriptExecutor, import them :

import org.openqa.selenium.JavascriptExecutor;

Try this :

WebElement chk = driver.findElement(By.xpath("//*[@class='custom-checkmark' and ./preceding-sibling::*[@id='terms_checkbox']]"));//or you have
String getPro;
getPro = ((JavascriptExecutor)driver).executeScript("return window.getComputedStyle(arguments[0], ':after').getPropertyValue('background-color');",chk).toString();
System.out.println(getPro);
chk.click();
Thread.sleep(1000);
getPro = ((JavascriptExecutor)driver).executeScript("return window.getComputedStyle(arguments[0], ':after').getPropertyValue('background-color');",chk).toString();
System.out.println(getPro);

Not sure with .getPropertyValue('background-color'), but this may be a clue.

frianH
  • 7,295
  • 6
  • 20
  • 45
  • Hi @Frian, thanks for the answer. I've updated my question with the JS code I'm using at the moment, targeting the display property instead but not working as it returns none on both cases even when the tick selected is displayed. – Francislainy Campos Aug 19 '19 at 10:31
1

Got the code to work. Targeting the input tag instead of span solved the problem. I had a mistake on my code when tried that first time so that's why though the isSelected field wasn't working and moved on to target the span tag instead which opened this thread here. Sorry about that and thanks for everybody's help.

Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81