1

I have few checkboxes on my page, with few of them checked by default. Through my code, I need to handle unchecking of all text boxes on the page and then proceed.

The problem is the checkboxes (both checked and un-checked) have exactly the same HTML / DOM structure with same attributes / values except for the "::after" appearing when the checkbox is checked. How do I write a locator to find out if the element is checked or unchecked, and then proceed to uncheck it.

<!-- When checkbox is unchecked -->
<label class="someclasslabel" on-click="[[event]]">
    <span>Male</span>
    <input type="checkbox" data-bind="checked: $properties.value">
    <span class="checkbox"></span>
</label>


<!-- When checkbox is checked -->
<label class="someclasslabel" on-click="[[event]]">
    <span>Male</span>
    <input type="checkbox" data-bind="checked: $properties.value">
    <span class="checkbox">
::after
</span>
</label>

I want a locator, so that I can get attribute if its checked or unchecked, and then uncheck it if already checked.

Ronnie
  • 251
  • 1
  • 3
  • 17

2 Answers2

0

You could try executing some Javascript on your checkbox element to get the value.

after_value = driver.execute_script
    ("return window.getComputedStyle(document.querySelector('.someclasslabel'),':after')
    .getPropertyValue('content');")

This is a bit of a hack, but worth a try.

CEH
  • 5,701
  • 2
  • 16
  • 40
0

After getting all the checkbox elements (with the same selector) no matter if they are checked or not, you can then iterate through them with a loop, and check if they are checked with the selected method. (C# reference here https://seleniumhq.github.io/selenium/docs/api/dotnet/ for RemoteWebElement.Selected Property). If .selected == true -》click; if not selected, do nothing;

Jeni
  • 1,088
  • 12
  • 30