xpathxpath-2.0domxpath

How do I select element with condition from other node


Suppose I have an HTML

<product-card>
  <card>
    <row-layout>
      <div>
        <div>
          <span>Product AAA</span>
        </div>
      </div>     
    </row-layout>
    <row-layout>
      <button>
        <span>
          <span>Create</span>
        </span>
      </button>
    </row-layout>
  </card>
  <card>
    <row-layout>
      <div>
        <div>
          <span>Product BBB</span>
        </div>
      </div>     
    </row-layout>
    <row-layout>
      <button>
        <span>
          <span>Create</span>
        </span>
      </button>
    </row-layout>
  </card>
</product-card>

So now I want to select <span>Create</span> with condition is <span>Product BBB</span> is it possible?

And what would be the correct XPath for this case, I am very new to XPath.


Solution

  • Something like this should work:

    //span[.="Create"][preceding::span[1][.="Product BBB"]]
    

    Target a span equals to "Create".
    The first span element preceding the targeted span element is equal to "Product BBB".