Webdriverio: Selector to find any element by text/partial text?

Created on 3 Oct 2017  路  4Comments  路  Source: webdriverio/webdriverio

The problem

I would like to query elements by text or partial text match without a restriction on element type. This is useful in cucumber steps such as /^I click the "(.*)" button$/, where the button could be implemented with an <a>, <button> or <input> tag.

I also may wish to look for text on a page e.g. "You have been logged out", but this could be embedded in almost any element, in which case it is not practical to enumerate all possible tags and search for the text with each one.

From the docs it doesn't look like this is possible without specifying an element type, is there a way? Can there be one?

Environment

  • WebdriverIO version: 4.8.0
  • Node.js version: 6.11.3
  • Standalone mode

All 4 comments

@peterjwest you can try *=some text. If that doesn't work you have to use xpath. There are tutorials on the internet on how to use xpath to query an random element with a specific text. Please join our Gitter for these kind of questions. This is not necessary an issue.

This is really high in Google results, so here's a working example:

browser.getText("//*[contains(text(),'Hello')]")

https://stackoverflow.com/a/2994336/4176104

Thanks!

*=some text is working for me (v4.13.2).

@christian-bromann why were you not sure that it would work? Was it related to the version of Webdriverio used?

Was this page helpful?
0 / 5 - 0 ratings