0

I am trying to check the status of the checkbox through a method which returns a boolean but it returns false in both cases (checked and unchecked).

 def checkbox_status(self):
        checked=self.driver.find_element_by_xpath('//span[contains(text(), "Dell")]/parent::span').is_selected()
        return checked

HTML

<div class="a-checkbox a-checkbox-fancy s-navigation-checkbox">
    <label>
        <input type="checkbox" name="" value="" checked="">
        <i class="a-icon a-icon-checkbox"></i>
        <span class="a-label a-checkbox-label">
            <span class="a-size-base a-color-base a-text-bold">Dell</span>
        </span>
    </label>
</div>
JeffC
  • 22,180
  • 5
  • 32
  • 55
Muthu Kumar
  • 33
  • 1
  • 13

4 Answers4

0

We can try with get attribute. That works fine for me:

def status():
    element =driver.find_element_by_xpath('//span[contains(text(), "Dell")]/ancestor::label/input')
    if element.get_attribute('checked') :
        return True
    else:
        return False
Sarthak Gupta
  • 392
  • 1
  • 10
0

You can use is_selected method that returns a boolean

To verify or get selection status, we can use two mechanisms

1.driver.find_element_by_id("privacypolicy").is_selected();

This will return ‘true’ if the checkbox is selected, false if it is not selected.

2.driver.find_element_by_id("privacypolicy").get_attribute("checked");

This will return ‘true’ if the checkbox is selected. But will return NoneType if checkbox is not selected.

for more information click on this link.

Pradnya Bolli
  • 1,915
  • 1
  • 19
  • 37
  • There is no element with an ID "privacypolicy" in OP's HTML. He's already using `.is_selected()`, why explain it to him? – JeffC Apr 02 '19 at 13:17
0

The problem is that your XPath is pointing to a SPAN instead of an INPUT. The INPUT is the checkbox here. If you switch your XPath to point to the INPUT, it should work just fine.

def checkbox_status(self):
    return self.driver.find_element_by_xpath('//input/following::span[.='Dell'][1]').is_selected()

XPath explanation

//input/following::span[.='Dell'][1]
^ find an INPUT
       ^ that is followed by a SPAN
                       ^ that contains the text "Dell"
                                 ^ return only the nearest one

NOTE: I combined the two lines of code since there's no need here to assign the element to a variable and then return that variable. Just return the result of the find.


To answer DebanjanB's question from the comments about the [1] in the XPath I'm using. Here's a simple demo. I start with the HTML

<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
</ul>

If I use the XPath //ul/li, it returns all 3 LIs. If I add a [1] to the XPath to give //ul/li[1], now it returns only the closest LI. This is useful when looking up or down the DOM. In some instances it isn't necessary because you can just use .find_element() (singular) but I've found that in IE this doesn't always work as expected. I haven't fully tested and documented the differences but I think it's basically that in some browsers if the position isn't in the XPath, it will return a collection where the first element is the closest element but in IE it will return the same collection but the first element is at the TOP of the DOM rather than the closest. After figuring that out the hard way, I started just putting the [1] inside the XPath and it seems to work consistently (at least on the browsers that I maintain).

The [1] I'm using is a alias for position(). You can read more about it on MDN.

JeffC
  • 22,180
  • 5
  • 32
  • 55
-2

I have used is_displayed() instead of is_selected() to assert the status of the checkbox. It is working fine.

def checkbox_status(self):
    checked=self.driver.find_element_by_xpath('//span[contains(text(), "Dell")]/parent::span/parent::label/input[@checked]').is_displayed()
    return checked
Sers
  • 12,047
  • 2
  • 12
  • 31
Muthu Kumar
  • 33
  • 1
  • 13
  • 1
    is_displayed return true if element is visible and do not have any relation to selected status. Please share HTML with selected and not selected statuses – Sers Apr 02 '19 at 11:14