我正在尝试获取标签内的信息。
<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 bygetText
. You can still access contents of the tag through JavaScript
所以你必须做这样的事情:
info.get_attribute('innerHTML')
关于javascript - Selenium Python 获取 <script> 标签信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50413206/