'How to return the content of the <script> element with python
I am trying to access the content of the script element of a generic website with Selenium.
<script>...</script>
.
url = 'https://unminify.com/'
browser.get(url)
elements = browser.find_elements_by_xpath('/html/body/script[1]')
type_ = [e.get_attribute('type') for e in elements]
text_ = [e.text for e in elements]
I get an empty list for text and attribute.
Solution 1:[1]
you can create a script element and access its contents with the variable of the create element + ".text"
OR
you can put an id for a script element and access its contents with document.getElementById("scriptId").text
(you should change "scriptId" to the id of the script)
var myJs = document.getElementById("myJs");
console.log(myJs.text);
<script id="myJs">
alert("This is myJs in action.");
</script>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
