AR Rahman
AR Rahman

Reputation: 33

How to create conditional xpath in selenium?

My HTML code looks like:

Table 1: And Table 2

<table id="ContentPlaceHolder_Body0">
    <tbody>
        <tr align="Center">
            <td style="width:250px;">Some Name</td>
        </tr>
    </tbody>
</table>
<table id="ContentPlaceHolder_Body1">
    <tbody>
        <tr align="Center">
            <td style="width:250px;">Some Name</td>
        </tr>
    </tbody>
</table>

When I use //*[contains(text(),'Some Name')] this xpath selects 2 values while I need to select only one value by Text.
I need to use that xpath with Text.
I'm trying to create xpath to get the "Some Name" from Table 2, element locator by using Text.
So I created this Xpath:

//table[@id='ContentPlaceHolder_Body1']//tr//td[@text()='Some Name']

This xpath is not working, need help to create correct xpath.

Upvotes: 0

Views: 188

Answers (2)

KunduK
KunduK

Reputation: 33384

You need to just remove the @

//table[@id='ContentPlaceHolder_Body1']//tr//td[text()='Some Name']

Upvotes: 0

JeffC
JeffC

Reputation: 25714

Given your HTML, this XPath works for me

//table[@id='ContentPlaceHolder_Body1']//td[text()='Some Name']

I think the issue with your XPath is that you were using @text() when it should be text().

//table[@id='ContentPlaceHolder_Body1']//tr//td[@text()='Some Name']
                                                ^ remove the @

Upvotes: 1

Related Questions