Python / Mechanize read data from select box

I want to make a script that can automatically fill in a web form. Now there is 1 field that will not work. It's the only field (selectbox) that gets filled with JavaScript. Other selectboxes are working fine.

This is my script:

import mechanize

url = "website URL"
br = mechanize.Browser(factory=mechanize.RobustFactory())
br.open(url)
br.select_form(nr=0)


br["firstName"] = "python"
br["lastName"] = "python2"
br["street"] = "teststreet"
br["nr"] = "1"
br["zip"] = "3321cc"
br["city"] = "city"
br["country"] = ["AF"]
br["email"] = "[email protected]"
br["password"] = "12345"
br["password_again"] = "12345"
br["signupForm_package"] = ["PACKAGE NAME"]  <- select box works

for i in range(0, len(br.find_control(type="checkbox").items)):
    br.find_control(type="checkbox").items[i].selected =True

# submit the request
response1 = br.submit()
print response1.read()

1 select box is filled within the HTML, which is no problem: I can read the text and it works.

The other select box is empty in the HTML, but gets filled with javascript (hides field when you select a different package etc.)

<select name="package_id" id="signupForm_package_id"></select> 

The JavaScript:

<script type="text/javascript">
$(document).ready(function() {
    var data = {"Sunny 5":{"1":{"id":"1","threads":"5","bandwidth":"5","price":1.79,"duration":"30"},"2":{"id":"2","threads":"5","bandwidth":"5","price":5.1,"duration":"90"},"3":{"id":"3","threads":"5","bandwidth":"5","price":9.66,"duration":"180"},"4":{"id":"4","threads":"5","bandwidth":"5","price":17.16,"duration":"365"},"29":{"id":"29","threads":"5","bandwidth":"5","price":0,"duration":"7"}},"Sunny 10":{"5":{"id":"5","threads":"5","bandwidth":"10","price":2.79,"duration":"30"},"6":{"id":"6","threads":"5","bandwidth":"10","price":7.95,"duration":"90"},"7":{"id":"7","threads":"5","bandwidth":"10","price":15.06,"duration":"180"},"8":{"id":"8","threads":"5","bandwidth":"10","price":26.76,"duration":"365"},"30":{"id":"30","threads":"5","bandwidth":"10","price":0,"duration":"7"}},"Sunny 20":{"9":{"id":"9","threads":"10","bandwidth":"20","price":3.79,"duration":"30"},"10":{"id":"10","threads":"10","bandwidth":"20","price":10.8,"duration":"90"},"11":{"id":"11","threads":"10","bandwidth":"20","price":20.46,"duration":"180"},"12":{"id":"12","threads":"10","bandwidth":"20","price":36.36,"duration":"365"},"31":{"id":"31","threads":"10","bandwidth":"20","price":0,"duration":"7"}},"Sunny 30":{"13":{"id":"13","threads":"10","bandwidth":"30","price":4.79,"duration":"30"},"14":{"id":"14","threads":"10","bandwidth":"30","price":13.65,"duration":"90"},"15":{"id":"15","threads":"10","bandwidth":"30","price":25.86,"duration":"180"},"16":{"id":"16","threads":"10","bandwidth":"30","price":45.96,"duration":"365"},"32":{"id":"32","threads":"10","bandwidth":"30","price":0,"duration":"7"}},"Sunny 60":{"17":{"id":"17","threads":"15","bandwidth":"60","price":5.79,"duration":"30"},"18":{"id":"18","threads":"15","bandwidth":"60","price":16.5,"duration":"90"},"19":{"id":"19","threads":"15","bandwidth":"60","price":31.26,"duration":"180"},"20":{"id":"20","threads":"15","bandwidth":"60","price":55.56,"duration":"365"},"33":{"id":"33","threads":"15","bandwidth":"60","price":0,"duration":"7"}},"Sunny 90":{"21":{"id":"21","threads":"15","bandwidth":"90","price":6.79,"duration":"30"},"22":{"id":"22","threads":"15","bandwidth":"90","price":19.35,"duration":"90"},"23":{"id":"23","threads":"15","bandwidth":"90","price":36.66,"duration":"180"},"24":{"id":"24","threads":"15","bandwidth":"90","price":65.16,"duration":"365"},"34":{"id":"34","threads":"15","bandwidth":"90","price":0,"duration":"7"}},"Sunny UNL":{"25":{"id":"25","threads":"20","bandwidth":"unlimited","price":7.79,"duration":"30"},"26":{"id":"26","threads":"20","bandwidth":"unlimited","price":22.2,"duration":"90"},"27":{"id":"27","threads":"20","bandwidth":"unlimited","price":42.06,"duration":"180"},"28":{"id":"28","threads":"20","bandwidth":"unlimited","price":74.76,"duration":"365"},"35":{"id":"35","threads":"20","bandwidth":"unlimited","price":0,"duration":"7"}}};

    $('#signupForm_payment_method').change(function() {
        var val = $(this).val();

        if(val == 'ideal') {
            $('#show-ideal').show();
        } else {
            $('#show-ideal').hide();
        }

        $.pepperEqualizer();
    });

    var setPackageSelect = function (id) {
        var packages = data[id];
        var periodHolder = $('#signupForm_package_id');

        periodHolder.html('');

        $.each(packages, function(key, value) {
            var text = '';
            if(value.duration == 7) {
                text = '1 week (' + value.duration + ' days) free';
            } else {
                if (value.duration == 30) {
                    text = '1 month';
                } else if (value.duration == 365) {
                    text = '1 year';
                } else {
                    text = (value.duration / 30) + ' months';
                }
                text += ' (' + value.duration + ' days)';

                var months = Math.floor(value.duration / 30);

                var price = value.price / months;
                text += ' à €' +  String(price.toFixed(2)).split('.').join(',') + ' p/month';
            }

            var option = '<option value="' + key + '" data-price="' + value.price.toFixed(2) + '" data-duration="' + value.duration + '">' + text + '</optiopn>';
            periodHolder.append(option);
        });


        $('#signupForm_package_id').trigger('change');
    };

    $('#signupForm_package').change(function() {
        var val = $(this).val();
        setPackageSelect(val);
    });

    $('#signupForm_package_id').change(function() {
        var element = $(this).find('option:selected');
        $('#signup-price').html('€ ' + String(element.data('price')).split('.').join(','));

        var duration = element.data('duration');
        if(duration == 7) {
            $('#payment-holder').hide();
            $('#signupForm_payment').val(0);
        } else {
            $('#payment-holder').show();
            $('#signupForm_payment').val(1);
        }
    });

                        $('#signupForm_package').val('Sunny UNL').trigger('change');
        });

My question is: Is it possible to read the data from that selectbox? I already tried something like this:

br.find_control("signupForm_package_id").get("1 week (7 days) free").selected = True

Answers:

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.