0

Given this is my DOM, and it doesn't belong to me so I may not modify it in any way, how do I select the correct "click here" link based on the product-id inner text? Note that there are 3 products in this code and the link can appear at any place in the page so I must be able to select solely based on product-id. This is for a behat/mink functional test for PHP and so far every attempt I have made to select based on product id has failed.

<div id = "container">
    <div class = "product">
        <div class="product-detail">
            <span class = "product-id">123-456-789</span>
        </div>
        <div class = "leftblock">
            <div class = "product-info">
                <a><span>Click Here</span></a>
            </div>
        </div>
    </div>

    <div class = "product">
        <div class="product-detail">
            <span class = "product-id">456-789-012</span>
        </div>
        <div class = "leftblock">
            <div class = "product-info">
                <a><span>Click Here</span></a>
            </div>
        </div>
    </div>

    <div class = "product">
        <div class="product-detail">
            <span class = "product-id">345-678-901</span>
        </div>
        <div class = "leftblock">
            <div class = "product-info">
                <a><span>Click Here</span></a>
            </div>
        </div>
    </div>
</div>

This xpath, for example, has failed:

//span[text()='Click Here']/following::span[@class='product-id' and text()='" . $productId . "']

I've tried using ancestor and other things but nothing yet. I'm not even sure if I am using the correct characters. Any ideas?

4

2 回答 2

0

I figured it out. I can select the product-id class then use /.. to go up a level to the parent then use / to go back down to where the proper target is.

于 2013-07-23T04:37:23.197 回答
0

Try the following xpath. Replace the product id with new product id. Hope this helps

//div[div/span[text() = '123-456-789']]/descendant::a/span[text() = 'Click Here']

于 2013-07-23T06:43:03.157 回答