Home > Blockchain >  How to get the text of an element as the web shows?
How to get the text of an element as the web shows?

Time:02-19

Im doing some scrapping with selenium Python, my problem is that, when I call WebElement.text() it gives me a string in one line with no format. But I want to get that text just as the web shows, that is, with the line breaks.

For example, the element with text:

<br>'Hello this is an example'<br>

In the web it shows as:

<br>
'Hello this is an<br> 
example'

I want the second result, but Selenium gives me the first one. I tried to 'manually' give format to the text using the width of the words with PIL, but the results are quite unexact.

CodePudding user response:

Instead of using the text attribute, you need to use the get_attribute("innerHTML") as follows:

print(WebElement.get_attribute("innerHTML"))

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python


References

Link to useful documentation:

  • Related