Oxymoron88
Oxymoron88

Reputation: 495

Getting Refreshed Highcharts Data Using Selenium & Python

I'm trying to access the chart data (high chart format) from the below website using Python & Selenium. The default "1 year" option works perfect, but when I use Selenium to click "5Y" option in chart & get data, it still returns the "1Y" information.

import time
from selenium import webdriver

website = 'https://www.moneycontrol.com/nps/nav/lic-pension-fund-scheme-g-tier-ii/SM003010'

# Open Website
driver = webdriver.Firefox()
driver.get(website)
time.sleep(2)

# Click on 5 Year Option in Chart
driver.find_element_by_id("li_5y").click()
time.sleep(2)

# Get Data from Highcharts Series
output = driver.execute_script('return window.Highcharts.charts[2].series[0].options.data')
driver.close()

I've also tried an alternative for clicking 5 year data but the same issue persists:

driver.execute_script("get_stock_graph('','5Y','li_5y','fiveymfd_5')")

Any advice would be appreciated on how to get the refreshed driver page info.

Thanks!

Upvotes: 0

Views: 382

Answers (1)

ppotaczek
ppotaczek

Reputation: 39099

On that page, every time you change a time period a new chart is created, so you need to get the data from the last one in Highcharts.charts array:

output = driver.execute_script('return window.Highcharts.charts[window.Highcharts.charts.length-1].series[0].options.data')

API Reference: https://api.highcharts.com/class-reference/Highcharts#.charts

Upvotes: 2

Related Questions