khanam khanam - 1 year ago 105
CSS Question

Getting text following a label in XPath

I am trying to do some tests using Selenium and I am facing some problems. Suppose I have the following:

<div class="itemize-row">
<p class="subText">
<span class="item-label">Card Color:</span> Mandarin
<span class="item-label">Colored Mug:</span> Red
</p>
</div>


Could anybody tell me how to retrieve data "Red" using XPath or CSS?

Answer Source

This XPath,

//span[@class='item-label' and .='Colored Mug:']/following-sibling::text()[1]

will return "Red" as requested.


Generally speaking, yes, but in Selenium, you cannot point to the text nodes. XPath expressions have to point to "elements". – alecxe

Ok, to account for Selenium limitations, this XPath,

substring-after(//span[@class='item-label' and .='Colored Mug:']/.., 'Colored Mug:')

which takes advantage of the fact that your target is at the end of the string value of the parent of the label, will also return "Red" as requested.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download