Selenium: How to assert all checkboxes checked?
Try :
Assert.IsTrue(selenium.IsChecked("xpath=(//input[@type='checkbox'])[" + i + "]"));
Note the addition of ()
.
The ()
says evaluate me first (as you would expect). This allows you to do (...)[1]
to find the first element of the xpath evaluated in the ()
.
Maya
Updated on June 04, 2022Comments
-
Maya almost 2 years
Decimal totalCheckboxes = selenium.GetXpathCount("//input[@type='checkbox']"); for (int i = 1; i < totalCheckboxes + 1; i++) { // Assert attempt 1 //Assert.IsTrue(selenium.IsChecked("/descendant-or-self::input[@type='checkbox'][" + i + "]")); // Assert attempt 2 //Assert.IsTrue(selenium.IsChecked("//input[@type='checkbox'][" + i + "]")); }
I need to assert multiple checkboxes are checked. The number of checkboxes are not always fixed because they depend on my search criteria. Also, the checkboxes have different id and name. For example, for the first checkbox, id = "ctl07_ctl01_ctl01_cbxRepeater_e5962e80-ca07-42e3-908f-1217ef5787d4" name = "ctl07$ctl01$ctl01$cbxRepeater_e5962e80-ca07-42e3-908f-1217ef5787d4"
and for the second checkbox, id="ctl07_ctl01_ctl03_cbxRepeater_c094f428-7ead-4ded-a11b-5824be49a95b" name="ctl07$ctl01$ctl03$cbxRepeater_c094f428-7ead-4ded-a11b-5824be49a95b"
and so on for the following checkboxes.
I have tried several things to add an assertion to assert the checkboxes are checked (Assert attempt 1 and Assert attempt 2 above), but when I run the test, it still fails at that point. Ther error I get:
Selenium.SeleniumException: ERROR: Element /descendant-or-self::input[@type='checkbox'][1] not found Selenium.SeleniumException: ERROR: Element //input[@type='checkbox'][2] not found
Any help on this would be highly appreciated. Thanks in advance!
-
mark-cs almost 13 years@Maya what was the error output ? Same as last time, that it couldn't find the element ?
-
Wesley Wiser almost 13 yearsActually
[@type='checkbox'][1]
is valid syntax. The W3 Spec uses this in several places (for instance, in the examples under section 2.5 ). -
mark-cs almost 13 years@Wesley Wiser you are correct, I don't know why I thought it wasn't but I have found in the past that format does not work with selenium, but using () does. I have updated my answer to remove the inaccuracy.
-
Ross Patterson almost 13 years@Maya: You need to add "xpath=" to the start of any Selenium XPath locator that you wrap in parentheses (e.g.,
"xpath=(//input[@type='checkbox'])[" + i + "]"
). When a locator starts with "/", Selenium assumes it's an XPath locator, but when it starts with "(", it doesn't know that. -
Ross Patterson almost 13 years@Wesley Wiser: Yes,
[xxx][yyy]
is valid syntax, but it doesn't do what Maya needs. The spec[1] does saypara[@type="warning"][5]
selects the fifth para child of the context node that has a type attribute with value warning, but that's because the two predicates are combined with an "and", not because the result of the first predicate is indexed by the second. By wrapping the first expression in parentheses, the result becomes a nodelist, and the second predicate acts as a sub-selector, not an and-ed condition. [1]:w3.org/TR/xpath/#path-abbrev -
Wesley Wiser almost 13 years@Ross Paterson Interesting, I hadn't realized that. Thanks for pointing it out!
-
Maya almost 13 years@Ross: OMG!! Thanks you so much!!!!! I have been working on this one test for more than a week, and you finally solved my problem!! Thanks a lot!! Adding the "xpath=" worked!!
-
Michael Durrant about 11 yearsNote that it's starting with
/
or//
that is really the key (not the 'xpath=' bit). I used to always usexpath=()
, e.g.xpath=(//div/id)
but now I just use//div/id