Reputation: 686
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
Reputation: 1
locate("//button[contains(text(), 'Start')]").first()
or
locate("//button[contains(text(), 'Start')]").at(1)
Works fine.
Upvotes: 0
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
Reputation: 11
You need using XPath for that
//button[1][contains(text(), 'Start')]
Upvotes: 1