admin管理员组

文章数量:1401607

I'm trying to get the data from a Highcharts chart using Selenium. My issue is that the setExtremes function does not work with .options.data. How can I read data after using setExtremes using purely Python-based methods?

My code:

capabilities = webdriver.DesiredCapabilities().FIREFOX
capabilities["marionette"] = True
driver = webdriver.Firefox(capabilities=capabilities, executable_path=gecko_binary_path)
driver.get(website)
time.sleep(5)

temp = driver.execute_script('return window.Highcharts.charts[0].series[0]'
                             '.xAxis[0].setExtremes(Date.UTC(2017, 0, 7), Date.UTC(2017, 0, 8))'
                             '.options.data'
                            )

data = [item for item in temp]
print(data)

I'm trying to get the data from a Highcharts chart using Selenium. My issue is that the setExtremes function does not work with .options.data. How can I read data after using setExtremes using purely Python-based methods?

My code:

capabilities = webdriver.DesiredCapabilities().FIREFOX
capabilities["marionette"] = True
driver = webdriver.Firefox(capabilities=capabilities, executable_path=gecko_binary_path)
driver.get(website)
time.sleep(5)

temp = driver.execute_script('return window.Highcharts.charts[0].series[0]'
                             '.xAxis[0].setExtremes(Date.UTC(2017, 0, 7), Date.UTC(2017, 0, 8))'
                             '.options.data'
                            )

data = [item for item in temp]
print(data)
Share Improve this question edited Jan 30, 2021 at 11:01 Deepak Rai 2,2033 gold badges23 silver badges38 bronze badges asked Jan 20, 2018 at 21:27 BlackBlack 4,6548 gold badges43 silver badges59 bronze badges 10
  • Does this JS work in the browser using the dev console? – JeffC Commented Jan 21, 2018 at 0:23
  • 4 Relevant HTML please. – undetected Selenium Commented Jan 21, 2018 at 12:26
  • @JeffC no it doesn't. t may be that the order of operations is off i.e. setExtremes must be called during the series but I've exhausted all efforts on my end. – Black Commented Jan 22, 2018 at 7:56
  • 5 You can bine two statement with semicolon in a single execute script as follows temp = driver.execute_script(' window.Highcharts.charts[0].xAxis[0].setExtremes(Date.UTC(20‌​17, 0, 7),Date.UTC(2017, 0, 8));return return window.Highcharts.charts[0].series[0].options.data') – Murthi Commented Jan 23, 2018 at 10:16
  • 2 It depends on how the change of bound is handled. If the data are already cached then use two statements as @Murthi suggested. If not, the chart will probably have to request the server to get the data which mean that you'll have to wait for the data to be refreshed with execute_sync_script. Note that you can get the cached data via Highcharts.charts[0].userOptions.series[0].data. – Florent B. Commented Jan 23, 2018 at 14:01
 |  Show 5 more ments

2 Answers 2

Reset to default 1

I am trying to execute your code on Highcharts demo page
The problem is with xAxis[0], xAxis is not an array but a dictionary, so you must supply a string value there inside those [].

check xAxis in the docs
I am guessing you're looking for xAxis.events.setExtremes

Edit

I see now that xAxis can be an array, but you're most likely missing those events so my solution should be changed to xAxis[0].events.setExtremes

The problem is setExtremes(min, max) method returns undefined, so you can not chain options. Solution is to wrap this method and pass on context, for example:

(function(H) {
  H.wrap(H.Axis.prototype, 'setExtremes', function (proceed) {
    proceed.apply(this, Array.prototype.slice.call(arguments, 1);
    return this; // <-- context for chaining
  });
})(Highcharts);

Now we can use:

return window.Highcharts.charts[0].xAxis[0].setExtremes(min, max).series[0].options.data;

Note: The snippet can be placed in a separate file and used like any other Highcharts plugin (simply load after Highcharts library).

Important

Axis object has references only to series that are bound to this axis. If you want to access any series on the chart use:

return window.Highcharts.charts[0].xAxis[0].setExtremes(min, max).chart.series[0].options.data;

本文标签: javascriptPython Read data from Highcharts after setExtremeStack Overflow