How to retrieve JSON from an element with Selenium?

Question:

I know you do this to retrieve text from an element,

elem = driver.find_element_by_css_selector('#__NEXT_DATA__')
text = elem.text

But how do you retrieve JSON from an element?
I have searched and most of the results are about JSON responses which seems to me to be a different thing?

Here is a snippet of the original HTML. It’s behind my account otherwise I would share the website.

<script id="__NEXT_DATA__" type="application/json">
{"props":{"middlewareResults":[{"account":{"user":{"nick":"deleted","userId":"deleted","isProUser":true,"isVerified":false,"url":"deleted","countryCode":"sn","pin":{"url":"deleted","anchor":"center-center","isDefault":false,"path":"deleted"},"deleted":{"division":"None","level":0},"deleted": 

https://i.imgur.com/f7gN5Wu.png

Asked By: user12759363

||

Answers:

You have to get the text from the script element then parse it as JSON

import json
#...
elem = driver.find_element_by_css_selector('#__NEXT_DATA__')
jsontext = json.loads(elem.get_attribute('innerHTML'))
Answered By: Dan Mullin