3

I have various buttons and several buttons with the same name "Start". I need to click on the first found button with this name (innerHTML). With jQuery this works with :

$('button:contains(Start):first').click()

How does it work with I.click()-Selector in CodeceptJS? I can't find the right syntax and always getting:

"invalid selector: An invalid or illegal selector was specified"

Here is the API for this function: https://github.com/Codeception/CodeceptJS/blob/master/docs/webapi/click.mustache

The only working solution I found is:

I.click('//button[1]');

But this solution is confusing, because you need to know the exactly number in the order of this element - and I have a lot of buttons with different names. Also this not allows me to search by innerHTML such as "Start".

Meteor Newbie
  • 556
  • 1
  • 6
  • 22

2 Answers2

2

You could use the I.executeScript like this:

I.executeScript("var elements = document.getElementsByName('Start');elements[0].click();"); or
I.executeScript("var elements = 
document.querySelector(\"button[name*='Start']\");elements[0].click();");
NithyaViji
  • 106
  • 6
1

You need using XPath for that

//button[1][contains(text(), 'Start')]

  • Do I use this function wrong: `I.click("//button[1][contains(text(), 'Start')]");` ? Because I get _" Clickable element //button[1][contains(text(), 'Start')] was not found by text|CSS|XPath"_ – Meteor Newbie Feb 03 '17 at 14:01
  • 1
    in chrome dev tools on `Elements tab` try `ctrl + f` for `//button[1][contains(text(), 'Start')]`, there you can check search pattern, maybe `Start` should be in lowercase? – Ihor Rusinko Feb 03 '17 at 18:07
  • This xpath is an incorrect, because of find some text in first button. Expected result is finding button with some text and getting first node from founded nodes: ```//button[contains(text(), 'Start')][1]``` – fpsthirty Mar 16 '19 at 13:04