Reputation: 469
In Webdriver.io, is there a way to click (an element by its) text?
e.g. a person has clicked and opened a drop-down menu and now wishes to click and select the desired element by its text.
Upvotes: 3
Views: 5554
Reputation: 526
You can pick any element with a text using xpath selectors. So you go like this:
browser.$('//*[text()="text you are looking for"]').click()
The "//" stands for any part of the HTML document object model (https://www.w3schools.com/js/js_htmldom.asp)
*
represents any HTML tag. If there is a <div>
you can use a //div, if there is <p>
you can use //p.
If you want to use just a part of the text you can use contains like this:
browser.$('//*[contains(text(), "part of text"]').click()
You can find about more about selectors in webdriver.io here: https://webdriver.io/docs/api/browser/$.html
And more about xpath in here: https://devhints.io/xpath
Upvotes: 0
Reputation: 756
.click('h1=Welcome to my Page')
http://webdriver.io/guide/usage/selectors.html Element with certain text
Upvotes: 7