sanaku
sanaku

Reputation: 309

Selenium - Trying to locate table cell by its contents using "contains"

This is the html code :

<div id="listMain" class="listMain">
    <table id="listMainTable" class="listTable" >
    <thead>
    <tbody id="mainTableBody">
        <tr id="Node0" class="row"      tabindex="0" >
        <tr id="Node1" class="alternateRow"     tabindex="-1" >
        <tr id="Node2" class="row"      tabindex="-1" >
        <tr id="Node3" class="alternateRow"     tabindex="-1" >
        <tr id="Node4" class="row"      tabindex="-1" >
            <td class="cell" >
            <td class="cell" >
                <div id="detailView_listColumn_4" style="overflow: hidden" aria-describedby="detailView_mainTooltip">TestReport</div>
            </td>
            <td class="cell" >
            <td class="cell" >
        <tr id="Node5" class="row"      tabindex="0" >
        <tr id="Node6" class="alternateRow"     tabindex="-1" >

</tbody>
</table>
</div>

I want to access the contents of row 5, column 2.

I am able to do this by directly accessing the cell giving the row and column number : driver.findElement(By.xpath("//table[@id='listMainTable']//tr[5]/td[2]"));

However, I want to access the cell by its contents using "contains".

I tried the following 2 ways :

driver.findElement(By.xpath("//table[@id='listMainTable']//tr[contains(td[1], 'TestReport')]/td[2]"));

driver.findElement(By.xpath("//table[@id='listMainTable']/tbody/tr/div[contains(text(), 'TestReport')]"));

Both throw the error - Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element.

I don't know if this has something to do with "TestReport" being contained within a "div" within the table cell. In that case, how can I access that particular cell using "contains" ?

Upvotes: 0

Views: 10764

Answers (2)

user1498953
user1498953

Reputation: 45

Your html is not well formatted. Several of the tags do not have corresponding closing tags.

You could use the following.

driver.findElement(By.xpath("/html/div[@id='listMain']/table[@id='listMainTable']/tbody[@id='mainTableBody']/tr[5]/td[2]/div[contains(text(),'TestReport')]"));

HTML

<html>
<div id="listMain" class="listMain">
<table id="listMainTable" class="listTable" >
<thead/>
<tbody id="mainTableBody">
    <tr id="Node0" class="row"      tabindex="0" />
    <tr id="Node1" class="alternateRow"     tabindex="-1" />
    <tr id="Node2" class="row"      tabindex="-1" />
    <tr id="Node3" class="alternateRow"     tabindex="-1" />
    <tr id="Node4" class="row"      tabindex="-1" >
        <td class="cell" />
        <td class="cell" >
            <div id="detailView_listColumn_4" style="overflow: hidden" aria-describedby="detailView_mainTooltip">TestReport</div>
        </td>
        <td class="cell" />
        <td class="cell" />
    </tr>
    <tr id="Node5" class="row"      tabindex="0" />
    <tr id="Node6" class="alternateRow"     tabindex="-1" />

</tbody>
</table>
</div>
</html>

Upvotes: 0

murali selenium
murali selenium

Reputation: 3927

You can try something like this

//div[contains(text(),'TestReport')]

Thank You, Murali

Upvotes: 1

Related Questions