2

I have two exactly the same buttons in two different forms on the same page. Forms and buttons have no id or name and the only difference is between those forms, which is the action.

<form class="form" method="post" action="http://mysite/one">
    <button title="" type="submit" class="button">Edit</button>
</form>

<form class="form" method="post" action="http://mysite/two">
    <button title="" type="submit" class="button">Edit</button>
</form>

I am writing acceptance tests in Codeception and perform those tests using PhpBrowser.

I want to click those buttons but only the first one is clicked.

This works only for the first button:

$I->click("button[type=submit]");



Solution 1:
I added IDs to the buttons.

Solution 2:

$I->click("//form[contains(@action,'http://mysite/two')]/button[@type='submit']");
kotsios
  • 59
  • 7
  • It's a good practice to add IDs to anything you might want to click with a Codeception Acceptance test. It's easier, and it makes your code a lot more readable (assuming that the ids are descriptive). – Bob Ray Jan 26 '19 at 05:20

2 Answers2

3

Try something like the following:

$I->click("//form[contains(@action,'http://mysite/two')]/button[@type='submit']");
luator
  • 4,769
  • 3
  • 30
  • 51
Houskov
  • 338
  • 1
  • 9
1

Try with the following XPath expression:

$I->click('(//button[@type=submit])[2]');
jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107