Search code examples
seleniumxpathautomation

Getting a value using xpath in <li>and <span> t


Needed to get this value using xpath. enter image description here

Have tried two different xpaths but along with this value "Model:" is also coming. The two xpaths I have tried are

//*[@id="description"]/ul/li[2] and //*[@id="description"]/ul/li[2]/text()

But same results.


Solution

  • Your first XPath expression selects the second li, which includes the span containing the text "Model:".

    In your second XPath expression you select the text node which is a direct child of the second li. That looks correct to me:

    //*[@id="description"]/ul/li[2]/text()
    

    I suspect that this is an issue with Selenium rather than with your XPath itself. Selenium may be selecting the li element anyway, rather than the text node. If that's true, you might have more luck with this:

    substring-after(//*[@id="description"]/ul/li[2], 'Model:')