Search code examples
javaselenium-webdriverautomated-tests

How to find elements using text without tag


HTML

<div class="cell">
<!---->
message.
<!----></div>

Trying to write an XPath using text then click on the element.

My locator

(//td[@class="el-table_1_column_5 el-table__cell"]//div[contains(text(),'')])[1]

but when I enter text in the above element, it is not found.

(//td[@class="el-table_1_column_5 el-table__cell"]//div[contains(text(),'message')])[1]

Solution

  • The problem is that the "message" text is likely not the first text node inside of the DIV. That's why

    (//td[@class="el-table_1_column_5 el-table__cell"]//div[contains(text(),'message')])[1]
    

    is not working. Change text() to . and I expect it will work.

    (//td[@class="el-table_1_column_5 el-table__cell"]//div[contains(.,'message')])[1]
    

    Read this on the difference between . and text() in XPath.