如何使用Python + Selenium从JavaScript IIFE检索变量的数据

时间:2019-05-09 19:26:05

标签: javascript python selenium

我正在尝试从“ var data”中获取值,但似乎无法弄清楚。使用其他线程,我发现我尝试使用execute_script返回数据,但是没有用。示例:

print(driver.execute_script("return data;")

上面给出了我的输出:

JavascriptException: javascript error: data is not defined

我可以使用相同的方法从“自定义”功能中获取一些信息:

print(driver.execute_script("return Custom;"))

这将返回:

{'doSomeStuff': {}, 'init': {}}

我尝试使用init并获得:

JavascriptException: javascript error: init is not defined

下面是带有JavaScript代码的HTML标记:

<script type="text/javascript">
    var Custom = function () {

        // private functions & variables
        var myFunc = function(text) {
            alert(text);
        };

        // public functions
        return {
            //main function
            init: function () {
                //initialize here something.
                //chart_practice_retention
                var data = [{'state': 'NY', 'active': 69, 'cancelled': 113},{'state': 'MI', 'active': 12, 'cancelled': 65},{'state': 'RI', 'active': 3, 'cancelled': 1},{'state': 'CA', 'active': 20, 'cancelled': 159},{'state': 'NJ', 'active': 29, 'cancelled': 23},{'state': 'WA', 'active': 4, 'cancelled': 35},{'state': 'AZ', 'active': 2, 'cancelled': 32},{'state': 'CO', 'active': 14, 'cancelled': 25},{'state': 'CT', 'active': 28, 'cancelled': 14},{'state': 'MA', 'active': 24, 'cancelled': 25},{'state': 'HI', 'active': 5, 'cancelled': 4},{'state': 'MD', 'active': 55, 'cancelled': 55},{'state': 'IL', 'active': 41, 'cancelled': 16},{'state': 'NV', 'active': 2, 'cancelled': 10},{'state': 'MN', 'active': 6, 'cancelled': 4},{'state': 'DC', 'active': 1, 'cancelled': 4},{'state': 'PA', 'active': 53, 'cancelled': 11},{'state': 'FL', 'active': 82, 'cancelled': 86},{'state': 'NH', 'active': 1, 'cancelled': 1},{'state': 'VT', 'active': 1, 'cancelled': 0},{'state': 'OH', 'active': 37, 'cancelled': 10},{'state': 'AR', 'active': 6, 'cancelled': 4},{'state': 'LA', 'active': 5, 'cancelled': 8},{'state': 'MO', 'active': 10, 'cancelled': 0},{'state': 'NM', 'active': 2, 'cancelled': 13},{'state': 'DE', 'active': 4, 'cancelled': 4},{'state': 'OK', 'active': 21, 'cancelled': 3},{'state': 'AK', 'active': 1, 'cancelled': 2},{'state': 'ME', 'active': 4, 'cancelled': 8},{'state': 'UT', 'active': 1, 'cancelled': 0},{'state': 'ND', 'active': 1, 'cancelled': 0},{'state': 'VA', 'active': 1, 'cancelled': 0},{'state': 'MT', 'active': 0, 'cancelled': 5},{'state': 'OR', 'active': 0, 'cancelled': 13},{'state': 'GA', 'active': 0, 'cancelled': 1},];
                var chart = AmCharts.makeChart("chart_practice_retention", {
                    "theme": "light",
                    "type": "serial",
                    "dataProvider": data,
                    "valueAxes": [{
                        "stackType": "3d",
                        // "unit": "practices",
                        "position": "left",
                        "title": "Practice Retention",
                    }],
                    "startDuration": 1,
                    "graphs": [{
                        "balloonText": "Cancelled practices in [[category]]: <b>[[value]]</b>",
                        "fillAlphas": 0.9,
                        "lineAlpha": 0.2,
                        "title": "Cancelled",
                        "type": "column",
                        "valueField": "cancelled"
                    }, {
                        "balloonText": "Active practices in [[category]]: <b>[[value]]</b>",
                        "fillAlphas": 0.9,
                        "lineAlpha": 0.2,
                        "title": "Active",
                        "type": "column",
                        "valueField": "active"
                    }],
                    "plotAreaFillAlphas": 0.1,
                    "depth3D": 60,
                    "angle": 30,
                    "categoryField": "state",
                    "categoryAxis": {
                        "gridPosition": "start"
                    },
                    "export": {
                        "enabled": true
                     }
                });
                jQuery('.chart-input').off().on('input change',function() {
                    var property    = jQuery(this).data('property');
                    var target      = chart;
                    chart.startDuration = 0;

                    if ( property == 'topRadius') {
                        target = chart.graphs[0];
                        if ( this.value == 0 ) {
                          this.value = undefined;
                        }
                    }

                    target[property] = this.value;
                    chart.validateNow();
                });

                //data tables
                $('table#active_practices').DataTable({
                    "order": [[ 4, "desc" ]],
                    "rowCallback": function( row, data, index ) {
                        $(row).addClass('clickable-row').click(function(){
                            window.location = "https://websiteremovedforprivacy.com;
                        });
                    }
                });
                $('table#inactive_practices').DataTable({
                    "order": [[ 5, "desc" ]],
                    "rowCallback": function( row, data, index ) {
                        $(row).addClass('clickable-row').click(function(){
                            window.location = "https://websiteremovedforprivacy.com;
                        });
                    }
                });
                            },

            //some helper function
            doSomeStuff: function () {
                myFunc();
            }

        };

    }();

    jQuery(document).ready(function() {    
       Custom.init();

    });
</script>

最终目标是从此变量中获取数据并将其拆分,以便为每个状态留给“ active”后面的数字。

谢谢!

编辑:如果有帮助,JavaScript图表将基于以下平台:http://www.amcharts.com/javascript-charts/

0 个答案:

没有答案