4

I have a table with checkboxes in each row. The user can select some rows and delete them, also I have this "select all" checkbox. I want to test selecting and deleting two rows for example, also click select all and delete them. So I need to check the second and third checkboxes for the first test and first one for the second test...

I couldn't find a way to select the nth element, the only way I could find is selecting by xpath, so I tried this without luck:

$I->checkOption("(//input[@type='checkbox'])[2]");

The error is smt. like //html(//input[@type='checkbox'])[2] is invalid. Apparently codeception is modifying the selector, so I tried to use WebDriverBy class but I couldn't find a way to pass it as a parameter...

Any ideas?

PS: This is not the actual case but a simple example to describe the problem...

madpoet
  • 1,023
  • 11
  • 28
  • Did you try to remove the bracket? "//input[@type='checkbox'][2]" – Algiz Jun 30 '14 at 09:23
  • @Algiz that won't work since each checkbox is inside a `td` element. I need the brackets unless the checkboxes were siblings... – madpoet Jun 30 '14 at 13:09
  • As far as I understand XPATH (which could be very tricky), the first part ("//input[@type='checkbox']") will build a list of nodes while the second part ("[2]") will take the second element of the list. It has no notion of sibling. The answer you gave is equivalent to mine. "//input" is a relative XPATH while "/descendant" is an absolute XPATH which will lokk in all nodes. – Algiz Jun 30 '14 at 15:08
  • Yeah it's definitely tricky :) This [answer](http://stackoverflow.com/a/4008925/286619) has a nice explanation of it... – madpoet Jul 01 '14 at 15:16

2 Answers2

3

I found a way to do it from this answer. This is it:

$I->checkOption("/descendant::input[@type='checkbox'][2]");
Community
  • 1
  • 1
madpoet
  • 1,023
  • 11
  • 28
1

This works at my end , you can use this one also

$I->checkOption('[name="published"]');

And for the Question:

we can find nth element in following ways:

  1. $I->click("table#races-table tbody tr:first-child td:last-child button")- Table(if you know first child and last child)

  2. $I->click('#side-menu li:nth-child(2)');- (Side menu you have and need to select item from the sub menu)

  3. $I->selectOption('.radio-tab:nth-child(1)', '1'); -- (Selecting radio button if you have multiple in a form)