0

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!

3
  • 1
    Hi @Oxymoron88, It looks like every time you change a time period a new chart is created. Please try to use this JS code: 'return window.Highcharts.charts[window.Highcharts.charts.length-1].series[0].options.data' Commented Jul 8, 2020 at 13:13
  • You sir, are a genius. Thank you so much! Could you please elaborate on what "charts.length-1" does? Commented Jul 9, 2020 at 3:13
  • 1
    It takes the last chart from the array. Commented Jul 9, 2020 at 8:51

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.