Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 528
doruksahin Avatar asked May 18 '18 14:05

doruksahin


People also ask

How to get text from tag name using selenium module in Python?

In this article, we will write a Python Script for getting the text from the tag name using selenium module. We will be getting the tag text from this URL. Step #2: Create a Chrome object or specify web driver path if it is not present in the default path and assign URL. Step #3: Specify the tag name, which you want to extract the text.

How do I use selenium with Python?

To get it, first you need to have selenium and the web driver install. You can let Python fire the web browser, open the web page URL and grab the HTML source. To start, install the selenium module for Python. It’s recommended that you do that in a virtual environment using virtualenv.

How do I get selenium markup in Python?

It is a markup language. To get it, first you need to have selenium and the web driver install. You can let Python fire the web browser, open the web page URL and grab the HTML source. To start, install the selenium module for Python.

How to handle web tables in selenium Python?

In this tutorial, we will learn How To Handle Web Tables in Selenium Python. A web table in an HTML document is defined under <table> tag. The rows of a table are represented with the <tr> tag and the columns in a table are represented with the <td> tag. Mostly each table has headers represented with the <th> tag.


1 Answers

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')
like image 134
Rolando Cruz Avatar answered Oct 18 '22 20:10

Rolando Cruz