I'm trying to wait for an element with certain css and inner text. I have multiple elements satisfying the css condition (element could be visible/invisible) and selenium ExpectedConditions is not behaving the way I want.
If I try to find elements by css first and then filter out myself, I sometimes miss the intended element because some elements might not have loaded.
By cssBy = By.css("CSS_HERE");
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(cssBy);
List<WebElement> tabs = driver.findElements(cssBy);
WebElement element =
tabs.stream().filter(tab -> tab.getText().contains("TARGET_TEXT")).findAny().get();
the above snippet sometimes misses the indented elements which satisfy the CSS but have not loaded when selenium checked for it. This results in me getting no matching element in second part.
I tried with textMatches and locator
By cssBy = By.css("CSS_HERE");
wait.until(ExpectedConditions.textMatches(cssBy, "TARGET_TEXT");
....
But I think the above snippet is selecting the first element it can find matching CSS and waits for its text to be TARGET_TEXT
which is not my intention.
Any suggestions to wait for text match in case of multiple elements matching the locator?