Question

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?

Was it helpful?

Solution

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']

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top