Home > Mobile >  Not able to fetch the right locator using xpath
Not able to fetch the right locator using xpath

Time:11-19

I tried some xpath and showing multiple results. I am expecting to find a locator of 'LinkA' button of color 'Red'. Is there any other xpath that i suppose to use here. Any solution please

this is my code:

<table>
<thead>
    <tr>
        <th>Color</th>
        <th>LinkName</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>
            <span>
            <button>
                <span>Red</span>
            </button>
            </span>
        </td>
        <td>
            <span>
            <button>
                <span>LinkA</span>
            </button>
            </span>
        </td>
    </tr>
    <tr>
        <td>
            <span>
            <button>
                <span>Yellow</span>
            </button>
            </span>
        </td>
        <td>
            <span>
            <button>
                <span>LinkA</span>
            </button>
            </span>
        </td>
    </tr>
    <tr>
        <td>
            <span>
            <button>
                <span>Green</span>
            </button>
            </span>
        </td>
        <td>
            <span>
            <button>
                <span>LinkA</span>
            </button>
            </span>
        </td>
    </tr>
</tbody>
</table>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

enter image description here

CodePudding user response:

The correct locator of 'LinkA' button of color 'Red' is as follows:

(//td/span/button/span)[2]

CodePudding user response:

This XPath should work:

//td//span[contains(text(),'Red')]]/following-sibling::td//span

Literally it finds td containing span containing Red text. Then if goes to the following sibling td containing the desired LinkA

  • Related