Splinter / Selenium未知错误:函数未在excute_script上定义

时间:2018-07-02 23:27:15

标签: python selenium selenium-chromedriver splinter

我正在尝试在网页的主体上定义的网页上执行功能。但是,碎片无法执行。这就是我试图做到的方式。

browser = Browser('chrome')
print("Visiting the Website")
browser.visit('https://www.zikanalytics.com/SearchCompetitor/Index')

print("Website Opened, Writing Down Username and Password")
browser.fill('Username', 'tsuna.october@gmail.com')
browser.fill('Password', 'Hiep1234')
print("Logging in")
browser.find_by_css(".btnregister").click()
time.sleep(3)
browser.visit('https://www.zikanalytics.com/SearchCompetitor/Index')
print("Filling in Username: ")
browser.find_by_css(".txtSearch").fill("acttradingpost")

browser.execute_script("DrawTable('chrono',30);)

DrawTable在页面主体中定义。分裂仍然引发以下错误

selenium.common.exceptions.WebDriverException: Message: unknown error: DrawTable is not defined

我还尝试通过调用一个包含该函数的.js文件并随后对其进行调用来添加该函数,但这仍然行不通。

browser.execute_script('var scriptElt = document.createElement("script");'\
'scriptElt.type ="text/javascript";'\
'scriptElt.src = "localhost/resource/script.js";'\
'document.getElementsByTagName("head")[0].appendChild(scriptElt); DrawTable('chrono', 30);')

这是script.js文件

  function DrawTable(competitor, range) {
        var table = $('#datatable-responsive').DataTable({
            "oLanguage": {
                sProcessing: "<img src='/images/gears.svg'>"
            },
            "order": [],


                dom: "<'row'<'col-sm-3'l><'col-sm-3'f><'col-sm-6'p>>" +
                "<'row'<'col-sm-12'tr>>" +
                "<'row'<'col-sm-5'i><'col-sm-7'p>>",

            "pageLength": 50,
            "bLengthChange": false,
            "bDestroy": true,
            "processing": true,
            "serverSide": true,
            "filter": false,
            "orderMulti": true,
            "Info": true,
            "emptyTable": "No items found.",
            "ajax": {
                "url": "/SearchCompetitor/LoadCompetitor/",
                "type": "POST",
                "data": {
                    competitor: competitor, drange: range, min: $(".minSelected").val(), max: $(".maxSelected").val(), minCurrentPrice: $("#minprofit").val(), maxCurrentPrice: $("#maxprofit").val(), LastSalePriceMin: $("#minsales").val(), LastSalePriceMax: $('#maxsales').val(), minamazon: $("#minamazon").val(), maxamazon: $('#maxamazon').val(), searchText: $('#searchTable').val()},
                "datatype": "json",
                "dataSrc": function (data) {
                    if (data.message == "ok") {
                        $(".resultsFor").text($('.range').val() + " days sales");
                        $('#tabelaData').show();
                        $('#datatable-responsive thead, #header').fadeIn("slow");
                        $('#emptyText').hide();
                        $('.flag').attr('src', '../../images/flags/' + data.CountryCode + '.svg');
                        $('.drzava2').text(data.CountryName);
                        $(".sellThrough").html('<span class="glyphicon-class">Sell-Through</span> <hr />' + data.SellThrough.toFixed(2) + ' %');
                        $(".activeListings").html('<span class="glyphicon-class">Active listings</span> <hr />' + data.ActiveListings);
                        $(".feedbackScoreClass").text(data.Feedback);
                        $(".feedbackScoreClass").val(data.Feedback);
                        $(".successfulListings").text(data.recordsFiltered);
                        $('.quantitySold').html('<span class="glyphicon-class ">Sold Items</span> <hr /> ' + data.QuantitySold);
                        $('.totalEarnings').html('<span class="glyphicon-class">Sale earnings</span> <hr /> ' + data.Currency + ' ' + data.TotalEarnings);
                        $('.averagePrice').text(data.Currency + ' ' + data.AveragePrice);

                        if (data.TotalScans != null && data.MaxScans != null) {
                            $("#smallText").text(data.TotalScans + ' / ' + data.MaxScans);
                        }
                        if (data.Added == null || data.Added == false) {
                            $('.divToWrite').html('<div class="col-lg-2 col-md-3 col-sm-12 col-xs-12" id="wholeBtn">   <a class="btn btn-app btnWatchList" style="min-width:100%;margin:0;height:67px"> <i class="fa fa-save" style="color:green"></i> Add to Watchlist  </a> </div>')
                        }
                        else if (data.Added == true) {
                            $('.divToWrite').html(' <div class="col-lg-2 col-md-3 col-sm-12 col-xs-12" id="addedToWatchList" style="padding:10px 0">  <h4 style="text-align:center;margin-top:20px"><i class="fa fa-check" style="color:darkseagreen;font-size:25px"></i> Watching</h4> </div>');
                        }
                        $("#titlerange").text(" " + $('.range').val() + " days ");
                        return data.data;
                    }
                    else if (data.message == "error") {
                        $("#emptyText").show();
                        $('#tabelaData').hide();
                        $('#header').hide();

                        ShowLongMessage("User doesn't exists");
                        return;
                    }
                    else if (data.message == "Empty") {
                        $("#emptyText").show();
                        $('#tabelaData').hide();
                        $('#header').hide();

                        ShowLongMessage("This user had no sales for past 30 days.");
                        return;
                    }
                    else if (data.message == "AddedToQueue") {
                        $("#emptyText").show();
                        $('#tabelaData').hide();
                        $('#header').hide();

                        ShowLongMessage("Thank you, this user has been added to our waiting queue. Please check back later while we fetch results for this user.");
                        return;
                    }
                    else if (data.message == "AlreadyAddedToQueue") {
                        $("#emptyText").show();
                        $('#tabelaData').hide();
                        $('#header').hide();

                        ShowLongMessage("This user is in waiting list. Please check back later for results.");
                        return;
                    }
                    else if (data.message == "NotPossible") {
                        $("#emptyText").show();
                        $('#tabelaData').hide();
                        $('#header').hide();

                        ShowLongMessage("Due to large volume of items in store, we are unable to fetch results for users with more than 110K items.");
                        return;
                    }
                    else if (data.message == "nosales") {
                        $("#emptyText").show();
                        $('#tabelaData').hide();
                        $('#header').hide();

                        ShowLongMessage("The seller had no sales for the selected period.");
                        return;
                    }
                }
            },


            "columns": [
                {
                    "sWidth": "5%",
                    "data": "ItemID",
                    "name": "SelectedColumnSales",
                    "render": function (data, type, row) {
                        return '<td class="" width="5%" tabindex="0"> <ul class="enlarge"> <li> <img src="https://i.ebayimg.com/images/i/' + data + '-0-1/s-l250/p.jpg" width="60px" height="60px"> <span> <img src="https://i.ebayimg.com/images/i/' + data + '-0-1/s-l250/p.jpg"> </span> </li> </ul> </td>';
                    },
                    "autoWidth": true
                },
                {
                    "sClass": "sorting_1",
                    "sWidth": "75%",
                    "targets": -1,
                    "data": "Title",
                    "name": "Title",
                    "render": function (data, type, row, full, meta) {

                        return '<td class="sorting_1" width="75%" data-url="https://www.ebay' + full.settings.json.Site + '/itm/' + row.ItemID + '" data-title="' + row.Title + '" data-price="' + row.CurrentPrice + '" data-image="https://i.ebayimg.com/images/i/' + data + '-0-1/s-l250/p.jpg" data-itemid="' + row.ItemID + '"> <h4><b><a href="https://www.ebay' + full.settings.json.Site + '/itm/' + row.ItemID + '" id="" target="_blank">' + data + '</a></b></h4> </td>';
                    },
                    "autoWidth": true,
                    'createdCell': function (td, cellData, rowData, row, col, data, response) {

                        $(td).attr('data-title', rowData.Title);
                        $(td).attr('data-price', rowData.LastSalePrice);
                        $(td).attr('data-url', rowData.LastSalePrice);
                        $(td).attr("data-image", "https://i.ebayimg.com/images/i/" + rowData.ItemID + "-0-1/s-l250/p.jpg");
                        $(td).attr('data-itemid', rowData.ItemID);
                        var url = "." + $(td).children("h4").children("b").children("a").eq(0).attr("href").split('.')[2].split('/')[0];
                        $(td).attr('data-url', "https://www.ebay" + url + "/itm/" + rowData.ItemID);

                    },
                },

                {
                    "sClass": "sorting_1",
                    "sWidth": "10%",
                    "targets": -1,
                    "data": "UploadDate",
                    "name": "UploadDate",
                    "render": function (data, type, row) {
                        var milli = data.replace(/\/Date\((-?\d+)\)\//, '$1');
                        var d = new Date(parseInt(milli));
                        return '<td class="sorting_1" style="text-align:left;" width="10%">' + data == null ? 'N/A' : getDateFormatted(d) + '</td>';
                    },
                    "autoWidth": true
                },
                {
                    "sClass": "sorting_1",
                    "sWidth": "7%",
                   // "targets": -1,
                    "data": "SelectedColumnSales",
                    "name": "SelectedColumnSales",
                    "render": function (data, type, row) {
                        return '<td class="sorting_1" style="text-align:left;" data-sales="' + data + '" width="7%">' + data + '</td>';
                    },
                    "autoWidth": true,
                    'createdCell': function (td, cellData, rowData, row, col, data, response) {
                        $(td).attr('data-sales', rowData.SelectedColumnSales);
                    }
                },
                {
                    "sClass": "sorting_1",
                    "sWidth": "10%",
                    "targets": -1,
                    "data": "QuantitySold",
                    "name": "QuantitySold",
                    "render": function (data, type, row) {

                        return '<td class="sorting_1" style="text-align:left;" width="10%">' + data + '</td>'
                    },
                    "autoWidth": true
                },
                {
                    "sClass": "sorting_1",
                    "sWidth": "10%",
                    "targets": -1,
                    "data": "CurrentPrice",
                    "name": "CurrentPrice",
                    "render": function (data, type, row,full) {
                        return '<td class="sorting_1" style="text-align:left;" width="10%">' + full.settings.json.Currency + ' ' + data + '</td>'
                    },
                    "autoWidth": true
                },
                {
                    "sClass": "sorting_1",
                    "sWidth": "10%",
                    "targets": -1,
                    "data": "LastSalePrice",
                    "name": "LastSalePrice",
                    "render": function (data, type, row,full) {
                        return '<td class="sorting_1" style="text-align:left;" width="10%">' + full.settings.json.Currency + ' ' + data + '</td>'
                    },
                    "autoWidth": true
                },
                {
                    "sClass": "tdforscript",
                    "render": function (data, type, row) {
                        return '<td style="display:none !important"></td>'
                    },
                    "autoWidth": true
                },
                {
                    "sClass": "sorting_1",
                    "targets": -1,
                    "data": "UPC",
                    "name": "Title",
                    "render": function (data, type, row, full) {
                        if (full.settings.json.Currency == "$") {
                            return '<div title="Find on Amazon" value="' + row.ItemID + '" class="btn btn-default btnAmazon"><i class="fa fa-bullseye" style="' + ((isParameterValid(row.UPC) || isParameterValid(row.EAN)) ? "color:#82f520" : "color:red") + '"></i></div>'
                        }
                        return '<div title="Find on Amazon" value="' + row.ItemID + '" class="btn btn-default btnAmazon"><i class="fa fa-bullseye" style="' + ((isParameterValid(row.UPC) || isParameterValid(row.EAN) || isParameterValid(row.MPN)) ? "color:#82f520" : "color:red") + '"></i></div>'
                    },
                    "autoWidth": true
                },
                {
                    "sClass": "sorting_1",
                    "sWidth": "3%",
                    "targets": -1,
                    "data": "Title",
                    "name": "Title",
                    "render": function (data, type, row) {
                        return '<td class="sorting_1" width="3%"><button type="button" class="btn btn-default" data-toggle="tooltip" data-placement="top" title="Scan Title" value="' + encodeURI(data) + '" data-original-title="Tooltip top"><i class="fa fa-bar-chart-o"></i></button> </td>'
                    },
                    "autoWidth": true
                }


            ]
        });
        $(document).on("click", "#searchBtn", function () {
            DrawTable($('.txtSearch').val(), $('.range').val());
        });
        $(document).on('keypress', "#searchTable", function (e) {
            if (e.which === 13) {
                DrawTable($('.txtSearch').val(), $('.range').val());
            }
        });
    }

有什么解决方法?

0 个答案:

没有答案