Search code examples
geb

GEB: how can I remove children elements in case of NonEmptyNavigator?


The following HTML:

<h1>
    "Bison 120"
    <span class="prise1">#1429</span>
</h1>

I need to extract exactly text from H1 - "Bison 120" Result of .text() is: "Bison 120#1429"

Very similar to this case: Using .text() to retrieve only text not nested in child tags but in GEB solution proposed for jQuery doesn't work.

My current idea for resolution - removing children from H1 element. Could you please help how can I remove children in GEB.

Thank you in advance!


Solution

  • You can simply get text of the parent and then subtract text of all children from it:

    def h1 = $("h1")
    
    def h1Text = h1.children().inject(h1.text()) { text, child ->
        text - child.text()
    }