doruksahin
doruksahin

Reputation: 386

Selenium Python getting <script> tag information?

I am trying to take information inside the tag.

<script type="text/javascript"> INFO </script>

More specifically:

<!DOCTYPE html>
<html lang="en" class="js logged-in client-root">
<head>...</head>
<body class style ="position: fixed; top: -265px; width: 100%;">
  <span id="react-root" aria-hidden="true">...</span>
  <script type="text/javascript> This is the tag i want. </script>
  <script type="text/javascript> 
     window.__initialDataLoaded(window._sharedData); </script>
...

I am trying this but no luck:

browser = webdriver.Chrome()
info = browser.find_element_by_xpath("//script[@type='text/javascript']")
print(info.text) //prints nothing

Upvotes: 12

Views: 14229

Answers (1)

Rolando Cruz
Rolando Cruz

Reputation: 2784

Seems like this is the expected behavior. See here:

This is working correctly. WebElement#getText returns the visible text that a user can see. The user cannot see text in a <script> tag, so it is not returned by getText. You can still access contents of the tag through JavaScript

So you will have to do something like this:

info.get_attribute('innerHTML')

Upvotes: 17

Related Questions