最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Selenium Python getting <script> tag information? - Stack Overflow

programmeradmin3浏览0评论

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

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
Share Improve this question edited May 18, 2018 at 15:13 Rolando Cruz 2,7841 gold badge17 silver badges24 bronze badges asked May 18, 2018 at 14:10 doruksahindoruksahin 3861 gold badge5 silver badges12 bronze badges 7
  • can you post the URL of the website that you are trying or the HTML code? – Ajithkumar_sekar Commented May 18, 2018 at 14:25
  • Update the question with a bit more of the outerHTML – undetected Selenium Commented May 18, 2018 at 14:26
  • instagram.com I am trying to extract comment times which are in <script> tag – doruksahin Commented May 18, 2018 at 14:31
  • @DebanjanB I didn't understand what you mean... – doruksahin Commented May 18, 2018 at 14:36
  • @doruksahin Update the HTML with more of its preceding and following tags – undetected Selenium Commented May 18, 2018 at 14:39
 |  Show 2 more comments

1 Answer 1

Reset to default 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')
发布评论

评论列表(0)

  1. 暂无评论