I'm trying to check if all td tags contain specified text using xpath like:

 WebElement tableId = driver.findElement(By.id("tablepress-6"));
 if (!driver.findElements(By.xpath("//td[contains(text(),'" + textInput + "')]")).isEmpty()) {

 List<WebElement> tdElements = tableId.findElements(By.xpath("//td[contains(text(),'"+ textInput + "')]"));

Also I've tried methods using xpath like:

List<WebElement> tdElements = tableId.findElements(By.xpath("//*[contains(text(),'" + textInput + "')]"));

And like:

List<WebElement> tdElements = tableId.findElements(By.xpath("//tr[*[text() = '"+ textInput + "']]/td[2]"));

But if I check my website I'm not getting all matches for td elements, maybe because of br elements inside some of matched td.

enter image description here

Instead of getting all 17 matches (like on the screen above), I'm getting less number of matches: enter image description here

Can someone suggest me the way how to get all td tags which contain specified text? Thanks in advance.

2 Answers 2


Try using .getAttribute("textContent") instead of getText

Because getText or text() actually uses innerText ,and will not detect text from hidden elements .

Don't get confused by the differences between Node.textContent and HTMLElement.innerText. Although the names seem similar, there are important differences:

textContent gets the content of all elements, including  and  elements. In contrast, innerText only shows “human-readable” elements.

textContent returns every element in the node. In contrast, innerText is aware of styling and won’t return the text of “hidden” elements.

Moreover, since innerText takes CSS styles into account, reading the value of innerText triggers a reflow to ensure up-to-date computed styles. (Reflows can be computationally expensive, and thus should be avoided when possible.)

Unlike textContent, altering innerText in Internet Explorer (version 11 and below) removes child nodes from the element and permanently destroys all descendant text nodes. It is impossible to insert the nodes again into any other element or into the same element after doing so


  • Thanks for the answer, I'll try to adapt it in my code. But for now I'm getting null, if I use: String tdTextMatches = tableId.getAttribute("Physics and astronomy") instead of List<WebElement> tdElements = tableId.findElements(By.xpath("//td[contains(text(),'Physics and astronomy')]"));
    – invzbl3
    Feb 22, 2020 at 13:34
  • @invzbl3 you have to use tableId.getAttribute("textContent") not the actual text
    – PDHide
    Feb 22, 2020 at 14:00
  • Okay, I got it. I've checked how it works and it gives me more content then I need. For me important is only specified text from td. I suppose the main problem is now how to get firstly List<WebElement> tdElements using correct xpath and only then to find something using like .getAttribute("textContent")
    – invzbl3
    Feb 22, 2020 at 17:38

I have struggled with case sensitive issue of Selenium. I have mixed text, but Selenium is case-sensitive, so I need to use whether lowercase or upper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.