12

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
7
  • can you post the URL of the website that you are trying or the HTML code? Commented May 18, 2018 at 14:25
  • Update the question with a bit more of the outerHTML Commented May 18, 2018 at 14:26
  • instagram.com I am trying to extract comment times which are in <script> tag Commented May 18, 2018 at 14:31
  • @DebanjanB I didn't understand what you mean... Commented May 18, 2018 at 14:36
  • @doruksahin Update the HTML with more of its preceding and following tags Commented May 18, 2018 at 14:39

1 Answer 1

17

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')
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot. By the way, I get page source and inspector from my browser. When i compare them something is different. Page source has commented time information but inspecting won't help me.
Sorry but can you please elaborate more on what you mean by "commented time information"?
I am scraping instagram. When im using Chrome manually, checked "Page source" and "Inspect element". I compared them and they wasnt the same. driver.page_source gave me "Inspect element" info, not "Page source". Is there a way i can scrap "Page source" with selenium?
If you want page source, then a scraper might be a better option. Try looking at BeautifulSoup (crummy.com/software/BeautifulSoup/bs4/doc)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.