0

I have below HTML code, in that I have to come up with XPath for the 2nd line of text: "Policy Count - 0".

<h2 id ="lblpremiumVol">
"Premium Volume - $"
<br>
"Policy Count - 0"
</h2>

How do I do that?

3
  • 1
    add the actual html , its not possible to tell without seeing the actual html structure
    – PDHide
    Jun 24 '20 at 10:21
  • <h2 id = "lblpremiumVol"> "Premium Volume - $" <br> "Policy Count - 0" </h2>
    – user45674
    Jun 24 '20 at 10:23
  • Can any one help me in this
    – user45674
    Jun 24 '20 at 10:50
4

Your xpath would be //h2/text()[2]. See the test here.

P.S. - I have no idea why you need that xpath (since you're not mentioning that in your question) but as a side note: you will not be able to use this xpath with Selenium since Selenium cannot wrap a text node into a WebElement

If you need that for Selenium, use the following approach:

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string title = (string)js.ExecuteScript("return document.evaluate('//h2/text()[2]', document, null, XPathResult.STRING_TYPE, null ).stringValue;");
6
  • thanks it works
    – user45674
    Jun 24 '20 at 11:16
  • You can mark my answer as correct
    – Alexey R.
    Jun 24 '20 at 11:20
  • but while running the scripts it throws an error, actually we have to compare two values which is equal or not(i.e, for one value i got a xpath and the other one is above)
    – user45674
    Jun 24 '20 at 11:26
  • 1
    Which script? Which error? What did work?
    – Alexey R.
    Jun 24 '20 at 11:28
  • 1
    @user45674 I think you need to raise another question because there are the details which have not been originally mentioned.
    – Alexey R.
    Jun 24 '20 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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