0

I would like to test a script on a website through python using the Selenium library. The only thing is that I have to modify the script first by removing a part of the script before running it. Below is the script and the part I would like to remove.

Part to remove

 //alert(data);
                if(data === 'BAD'){
                    $('.win_tr').show();
                    $('.winText').html('<img src="/images/icons/cross.png" /> Je hebt niet genoeg spins om aan het geluksrad te draaien!');
                    wheelSpinning = false;          // Reset to false to power buttons and spin can be clicked again.

Script

<script>
// Create new wheel object specifying the parameters at creation time.
var theWheel = new Winwheel({
    'numSegments'  : 12,     // Specify number of segments.
    'outerRadius'  : 212,   // Set outer radius so wheel fits inside the background.
    'textFontSize' : 18,    // Set font size as desired.
    'textDirection'   : 'reversed',
    'segments'     :        // Define segments including colour and text.
    [
       {'fillStyle' : '#eae56f', 'prize' : '0', 'text' : '10 Eerpunten'},
       {'fillStyle' : '#89f26e', 'prize' : '1', 'text' : '10 Credits'},
       {'fillStyle' : '#7de6ef', 'prize' : '2', 'text' : '250 Hoeren'},
       {'fillStyle' : '#e7706f', 'prize' : '3', 'text' : '$10.000.000'},
       {'fillStyle' : '#eae56f', 'prize' : '4', 'text' : 'Geen Prijs'},
       {'fillStyle' : '#89f26e', 'prize' : '5', 'text' : '50 Eerpunten'},
       {'fillStyle' : '#7de6ef', 'prize' : '6', 'text' : '50 Credits'},
       {'fillStyle' : '#e7706f', 'prize' : '7', 'text' : '$25.000.000'},
        {'fillStyle' : '#eae56f', 'prize' : '0', 'text' : '250 Hoeren'},
        {'fillStyle' : '#89f26e', 'prize' : '1', 'text' : 'Geen Prijs'},
        {'fillStyle' : '#7de6ef', 'prize' : '2', 'text' : '500 Hoeren'},
        {'fillStyle' : '#e7706f', 'prize' : '3', 'text' : '1000 Hoeren'}
    ],
    'animation' :           // Specify the animation to use.
    {
        'type'     : 'spinToStop',
        'duration' : 5,     // Duration in seconds.
        'spins'    : 8,     // Number of complete spins.
        'callbackFinished' : 'alertPrize()'
    }
});

// Vars used by the code in this page to do power controls.
var wheelPower    = 0;
var wheelSpinning = false;

// -------------------------------------------------------
// Function to handle the onClick on the power buttons.
// -------------------------------------------------------
function powerSelected(powerLevel)
{
    // Ensure that power can't be changed while wheel is spinning.
    if (wheelSpinning == false)
    {
        // Reset all to grey incase this is not the first time the user has selected the power.
        document.getElementById('pw1').className = "";
        document.getElementById('pw2').className = "";
        document.getElementById('pw3').className = "";

        // Now light up all cells below-and-including the one selected by changing the class.
        if (powerLevel >= 1)
        {
            document.getElementById('pw1').className = "pw1";
        }

        if (powerLevel >= 2)
        {
            document.getElementById('pw2').className = "pw2";
        }

        if (powerLevel >= 3)
        {
            document.getElementById('pw3').className = "pw3";
        }

        // Set wheelPower var used when spin button is clicked.
        wheelPower = powerLevel;

        // Light up the spin button by changing it's source image and adding a clickable class to it.
        document.getElementById('spin_button').src = "spin_on.png";
        document.getElementById('spin_button').className = "clickable";
    }
}

// -------------------------------------------------------
// Click handler for spin button.
// -------------------------------------------------------
function startSpin()
{
    // Ensure that spinning can't be clicked again while already running.
    if (wheelSpinning == false)
    {
        // Based on the power level selected adjust the number of spins for the wheel, the more times is has
        // to rotate with the duration of the animation the quicker the wheel spins.
        if (wheelPower == 1)
        {
            theWheel.animation.spins = 3;
        }
        else if (wheelPower == 2)
        {
            theWheel.animation.spins = 8;
        }
        else if (wheelPower == 3)
        {
            theWheel.animation.spins = 15;
        }

        // Disable the spin button so can't click again while wheel is spinning.
        document.getElementById('spin_button').src       = "spin_off.png";
        document.getElementById('spin_button').className = "";

        $.ajax({
            url: '/ajax/wheeloffortune.php?getnumber',
            type: 'post',
            data: {},
            dataTupe: 'json',
            success: function(data) {
                //alert(data);
                if(data === 'BAD'){
                    $('.win_tr').show();
                    $('.winText').html('<img src="/images/icons/cross.png" /> Je hebt niet genoeg spins om aan het geluksrad te draaien!');
                    wheelSpinning = false;          // Reset to false to power buttons and spin can be clicked again.
                }else{
                    resetWheel();
                    var stopAt = (data);

                    // alert(data);
                    theWheel.animation.stopAngle = stopAt;

                    // Begin the spin animation by calling startAnimation on the wheel object.
                    theWheel.startAnimation();

                    // Set to true so that power can't be changed and spin button re-enabled during
                    // the current animation. The user will have to reset before spinning again.
                    wheelSpinning = true;

                    $.ajax({
                        url: '/ajax/wheeloffortune.php',
                        type: 'post',
                        data: {start_spin:true},
                        dataTupe: 'json',
                        success: function(data) {

                        },
                        error: function (data) {
                            // alert('Something went wrong');
                            $('.win_tr').show();
                            $('.winText').html('<img src="/images/icons/cross.png" /> Er is iets fout gegaan!');
                            wheelSpinning = false;          // Reset to false to power buttons and spin can be clicked again.
                        }
                    });
                }
            },
            error: function (data) {
                // alert('Something went wrong');
                $('.win_tr').show();
                $('.winText').html('<img src="/images/icons/cross.png" /> Er is iets fout gegaan!');
                wheelSpinning = false;          // Reset to false to power buttons and spin can be clicked again.
            }
        });
    }
}

// -------------------------------------------------------
// Function for reset button.
// -------------------------------------------------------
function resetWheel()
{
    theWheel.stopAnimation(false);  // Stop the animation, false as param so does not call callback function.
    theWheel.rotationAngle = 0;     // Re-set the wheel angle to 0 degrees.
    theWheel.draw();                // Call draw to render changes to the wheel.

    // document.getElementById('pw1').className = "";  // Remove all colours from the power level indicators.
    // document.getElementById('pw2').className = "";
    // document.getElementById('pw3').className = "";
}

// -------------------------------------------------------
// Called when the spin animation has finished by the callback feature of the wheel because I specified callback in the parameters.
// -------------------------------------------------------
function alertPrize()
{
    // Get the segment indicated by the pointer on the wheel background which is at 0 degrees.
    var winningSegment = theWheel.getIndicatedSegment();

    // Do basic alert of the segment text. You would probably want to do something more interesting with this information.
    // alert("You have won " + winningSegment.text);

    var winText = winningSegment.text;
    var winPrize = winningSegment.prize;

    $.ajax({
        url: '/ajax/wheeloffortune.php',
        type: 'post',
        data: {text:winText, prize_won:winPrize},
        dataTupe: 'json',
        success: function(data) {

            var data = jQuery.parseJSON(data);

            // Show whole array
            var data2 = JSON.stringify(data);
            // console.log(data2);

            // De win tekst
            var winText = data['tekst'];
            var status = data['status'];
            var likes2 = data['prize'];

            var new_spins = $('.spins').text() - 1;
            if(new_spins < 0) {
                new_spins = 0;
            }
            $('.spins').html(new_spins);

            $('.win_tr').show();
            if(status = 'OK' && winText !== 'BAD') {
                $('.winText').html('<img src="/images/icons/tick.png" /> ' + winText + '');
            }else{
                $('.winText').html('<img src="/images/icons/cross.png" /> Er is iets fout gegaan!');
            }
            wheelSpinning = false;          // Reset to false to power buttons and spin can be clicked again.
        },
        error: function (data) {
            //alert('Something went wrong');
            $('.win_tr').show();
            $('.winText').html('<img src="/images/icons/cross.png" /> Er is iets fout gegaan!');
            wheelSpinning = false;          // Reset to false to power buttons and spin can be clicked again.
        }
    });
}

3
  • I have yet to see a selenium/python script that executes an ajax call. You may have to go through that line by line and convert it to python. Commented Jan 4, 2020 at 0:48
  • @Jortega Is there another way than selenium to get the same result? Commented Jan 4, 2020 at 0:51
  • ajax is actually just javascript so you could convert the ajax calls to javascript. See this example. stackoverflow.com/questions/33196546/… Commented Jan 4, 2020 at 2:08

0

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.