Meteor Newbie
Meteor Newbie

Reputation: 686

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

Upvotes: 3

Views: 4979

Answers (3)

R P
R P

Reputation: 1

locate("//button[contains(text(), 'Start')]").first() 

or

locate("//button[contains(text(), 'Start')]").at(1)

Works fine.

Upvotes: 0

NithyaViji
NithyaViji

Reputation: 121

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

Upvotes: 2

Ihor Rusinko
Ihor Rusinko

Reputation: 11

You need using XPath for that

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

Upvotes: 1

Related Questions