Skip to content
Advertisement

Selenium : Get text inside an element but not texts inside the nested tags within it

Lets say I have an element

JavaScript

When I am fetching the element by classname

JavaScript

This gives me

JavaScript

What I wanted was just ₹199.

Note that I can’t just format the text and get the first text on split by space as the structure of the page keeps changing.

Advertisement

Answer

Using little bit JS:

JavaScript

Output:

JavaScript

What we are doing with JS is we are fetching all the child nodes of our target div element. Then we are iterating through all of these nodes and getting textContent values from text nodes only. Simultaneously, we are adding all those values into a string type variable l. We return l from JS and strip it off of useless characters in Python. That’s it.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement