javascript - Selenium Python 获取 <script> 标签信息?

标签 javascript python selenium selenium-webdriver selenium-chromedriver

我正在尝试获取标签内的信息。

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

更具体地说:

<!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>
...

我正在尝试这个,但没有成功:

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

最佳答案

这似乎是预期的行为。参见 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

所以你必须做这样的事情:

info.get_attribute('innerHTML')

关于javascript - Selenium Python 获取 &lt;script&gt; 标签信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50413206/

相关文章:

javascript - 删除特定标记 APIv3

python - 如何将自定义 Django 用户模型注册到管理页面?

python - 如何使用python执行erlang命令

java - SoapUI项目执行后WebDriver异常

javascript - 在显示 twitter feed 时,如何使用 JavaScript 或 JQuery 检查特定推文是否被转发?

javascript - 需要 jQuery 时间表小部件

javascript - 设置wow js的掉落范围

python - 从特定行向下搜索 csv

java - ChromeDriver 会阻止并且在出现意外警报框后不会失败

javascript - 访问从 javascript 函数 webdriver 动态生成的 href