I am quite stuck while testing a web application. I am trying to check to ensure a class exists - in this case the class of "originalProductPrice" - out of a group of identical looking li items. Only some of them have this class and if one does I want to click the btn-primary (labeled See More below). Essentially what it feels like I need to do is find the originalProductPrice class - walk up to the top li parent and then drill back down to the button to make sure I am clicking the button associated with that class. However, I am lost as to how to do that. My closest attempt so far has been:

[When(@"I Click See More on the First Option with An Original Product Price")]
public void WhenIClickSeeMoreOnTheFirstOptionWithAnOriginalProductPrice()
    ReadOnlyCollection<IWebElement> packages = CurrentBrowser.FindElements
        (By.XPath("//span[@class = 'originalProductPrice:first-child']//tr"));
    ReadOnlyCollection<IWebElement> seeMore = packages.FindElements

Below is the corresponding HTML:

<li class="span4" id="Tile">
<div class="thumbnail">
    <div id="locationImage">
        <div id="sImage"></div>
    <div name="InvokeWhiteSpaceWrap">
        <span id="sName">Name</span>
        <span name="ratings">
            <span id="sMinRating"></span>
            <span id="sMaxRating"></span>
        <div id="sDescription">
        <table class="table table-condensed table-striped" id="pList">
                        <div name="pNameDiv">
                    <td class="nowrap">
                        <span name="pPrice">
                            <span class="beforePriceCurrency">$</span>
                            <span class="afterPriceCurrency"></span>
                        <span name="originalProductPrice" 
                            <span class="beforePriceCurrency">$</span>
                            <span class="afterPriceCurrency"></span>
    <a onclick="$.blockUI({ message: $('#waitMessage') });" 
        class="btn btn-block btn-link btn-small">
        More Options Available                                           
    <a onclick="$.blockUI({ message: $('#waitMessage') });" 
        class="btn btn-block btn-primary">See More</a>

Any help is greatly appreciated!

  • for some funny reason, your code was not shown properly, they went so far that some of them could not be seen. I noticed when I attempted to edit your code, I could see everything in the editing mode, not sure if this was a stackoverflow issue.
    – Yu Zhang
    Dec 13, 2017 at 21:25

2 Answers 2


Try the following.

IWebElement button = CurrentBrowser.FindElement(By.XPath("//span[@class='originalProductPrice']/ancestor::li//a[contains(@class,'btn-primary')]"));

The portion of note in this XPath is /ancestor::li. The XPath works by first finding the <span> element with class equal to "originalProductPrice", then searching for your button from the nearest <li> element that is a parent of that span.


Use below mentioned Xpath to locate your element:

//table[//span[@class='originalProductPrice']]/following::a[text()='See More']

Explanation :


Locate the <table> where it found class = originalProductPrice for the <span>

/following::a[text()='See More']

Locate the anchor tag which follows the above located table.


//table[//span[@class='originalProductPrice']]/../following-sibling::a[@class="btn btn-block btn-primary"]


//span[@class='originalProductPrice']/ancestor::table/following::a[text()='See More']

Also there can be different ways to locate the same element. you can go through some tutorial to get the idea about using following contains ancestor sibling parent etc in xpath. Refer this site or google the same topic.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.