-3

HTML

<span [@class="some class"]> 
    <h1> Some text 
        <a> another text </a> 
    </h1>
</span>

How can I write an XPath to get the text under h1 that doesn't contain the text inside the child a?

JeffC
  • 22,180
  • 5
  • 32
  • 55
Kate
  • 65
  • 1
  • 11
  • Can you update what exactly you mean by "h1" `besides` "a"? – undetected Selenium Aug 16 '17 at 12:02
  • @Kate I edited the question to clarify what I think the question is. Please adjust if my assumption is not correct. If my assumption *is* correct, you can't do this. Any attempt to pull text from the `h1` will get the text in the `a` also. One thing you can do is pull the text from `h1` and then remove text from contained elements, e.g. the `a`. – JeffC Aug 16 '17 at 14:43
  • Possible duplicate of [How to get text from parent element and exclude text from children (C# Selenium)](https://stackoverflow.com/questions/28945692/how-to-get-text-from-parent-element-and-exclude-text-from-children-c-selenium) – JeffC Aug 16 '17 at 14:44
  • Sorry, I mean get only "h1" and remove "a" from this text – Kate Aug 22 '17 at 08:42

2 Answers2

-1

The xpath for "Some text" is /h1/text() -- you need type text node.

Serge
  • 1,416
  • 1
  • 15
  • 21
-1

Try below XPath :-

//h1[not(self::a)]

OR

//span[@class='some class']//h1[not(self::a)]

Above XPath select everything excluding a tag

Hope it will help you :)

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125