codeceptioncodeceptjs

I.click()-selector in CodeceptJS - how to find first button with specific innerHTML


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".


Solution

  • 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();");