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 LI
s. 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.