Home > Net >  Accessing javascript data with selenium and python
Accessing javascript data with selenium and python

Time:02-24

I have a website that I want to scrape data from. This specific data is under a script tag and I cannot figure out how to access it.

<script type="text/javascript">
    window.gameData = {"a dict of data I would like to get"}
</script>

So far I have tried probably every combination of "find_element_by..." and I am beginning to wonder if this problem can even be solved. It doesn't allow me to use the "get_attribute", ".text", or any other helpful selenium function.

I even tried this function

#script = wd.find_element_by_xpath('//*[@id="js-hook-pz-moment__game"]/script/text()')

Which is the direct xpath to the javascript loaded data and it returned a quite scary error.

selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "//*[@id="js-hook-pz-moment__game"]/script/text()" is: [object Text]. It should be an element.

So thats my problem. Thank you for any and all help :)

CodePudding user response:

You would just do:

game_data = driver.execute_script("return window.gameData")
  • Related