Skip to content
Advertisement

How to get WebElement text with selenium

Please see the following element:

JavaScript

Find my element:

JavaScript

So after found this div and get the text using selenium with getText or getAttribute("innerHTML") the return:

JavaScript

So my question is how to get only the last line without this x

Advertisement

Answer

The text you want is present in a text node and cannot be retrieved directly with Selenium since it only supports element nodes.

You could remove the beginning :

JavaScript

You could also extract the desired content from the innerHTML with a regular expression:

JavaScript

Or with a piece of JavaScript:

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