0

I'm implementing test cases for aviasales.com web sites and I'm trying to validate state of the checkbox.

I could easily find the checkbox and even click on it via:

WebElement checkboxValue = driver.findElement(
      By.xpath("//label[@class='mewtwo-show_hotels__label']"));
            checkboxValue.click();

The problem is that boolean bool = checkboxValue.isSelected(); returns only false even it was selected.

I googled a lot and found an advice to use JavaScript for that, so I tried from the console before Java implementation but same thing happens - always false:

var field = document.getElementsByClassName("mewtwo-show_hotels__label");
if (field[0].checked == true) {alert("1")} else {alert("2")}

and I got only alert with 2 no matter what is a checkbox condition (selected or not)

What else can I try here if chrome locator identifies checkbox as ::before in source code?

enter image description here

Thank you!

Aleksey Kiselev
  • 331
  • 1
  • 7
  • 21

2 Answers2

0

just query for checkbox, and not for the label

you need to select element with mewtwo-show_hotels__*checkbox* class (not mewtwo-show_hotels__*label*)

you see label, but it is just styled to reflect hidden checkbox state

Andrii Muzalevskyi
  • 3,261
  • 16
  • 20
0

You should find the actual element for checkbox. What you have supposed to use is not correct, because it is label.

But there are elements on top of that:
Input with class - memtwo-hotels-checkbox and
Div with class - memtwo-custom_checkbox_wrapper
It is more likely there are some attributes which you can you to determine is element selected. If after that you cannot find any special attribute, then go to your DEV team and ask about providing the attribute.

You have custom checkbox, that is why any standard functions will not work. So come back to html code and try to find inside div some attribute which changes when you check and uncheck. When you find special attribute, then you need to do in java something like that:
if (element.getAttribute(MySpecialAttribute) != null) { //code here }

Hope it helps.

nick318
  • 575
  • 4
  • 18
  • They are not my Dev team, this is my study project.. Will try – Aleksey Kiselev Jan 16 '18 at 21:02
  • var field = document.getElementsByClassName("mewtwo-custom_checkbox"); if (field[0].checked == true) {alert("1")} else {alert("2")} returns 2 all the time – Aleksey Kiselev Jan 16 '18 at 21:06
  • You did not get the point. You have custom checkbox, that is why any standard functions will not work. So come back to html code and try to find inside div some attribute which changes when you check and uncheck. When you find special attribute, then you need to do in java something like that: `if (element.getAttribute(MySpecialAttribute) != null) { //code here } ` – nick318 Jan 16 '18 at 21:10
  • var field = document.getElementById('flights_show_hotels_id--whitelabel_en'); if (field.checked == true) {alert("1")} else {alert("2")} returns proper result! thank you! – Aleksey Kiselev Jan 16 '18 at 21:36