0

Currently I need to find elements, that have at least one or more words from some array.

Here is the code that I have:

collect(['some', 'words'])->each(function ($word) {
    $found = $this->driver->findElements(
        WebDriverBy::xpath("//div[contains(text(), '$word')]")
    );
});

Is is possible somehow to make it using one findElements() call and pass the array there?

Barry
  • 3,303
  • 7
  • 23
  • 42
D.R.
  • 2,540
  • 3
  • 24
  • 49
  • You could try building a regex? See https://stackoverflow.com/questions/21405267/xpath-using-regex-in-contains-function – lucaswxp Oct 22 '18 at 13:32
  • @lucaswxp my array does not have any pattern, so this may be quite unpleasant and hard to read afterwards. – D.R. Oct 22 '18 at 13:39
  • I don't understand. A simple join will do, you want the final output "(word1|word2|word3)" – lucaswxp Oct 22 '18 at 13:40
  • The number of words is different each time or hardcoded words? – Andersson Oct 22 '18 at 13:43
  • @lucaswxp Could you show an example? – D.R. Oct 22 '18 at 14:55
  • @Andersson hardcoded – D.R. Oct 22 '18 at 14:55
  • If there are couple words by which you need to find element, you can try `"//div[contains(text(), 'some') and contains(text(), 'words')]"` – Andersson Oct 22 '18 at 14:58
  • @D.R. Would you be able to port it to Php if I answer in Java? – Kireeti Annamaraj Oct 22 '18 at 15:26
  • You just need to use an XPath that consists of a bunch of OR conditions for each word in your collection of words. If you google, you will see some examples of how to use OR in XPath. Once you try that, update your question with the results if it doesn't work. – JeffC Oct 22 '18 at 15:31

0 Answers0