0

I am trying to simulate a button click in Python on a website, when that button is clicked it adds some stuff to the end of the URL and returns false. If I add the stuff to the end of the URL it takes me to the right place but doesn't register that the button was click which is why I think I need to use the return false part.

Here is the exact code for the button and the button area. I don't know if this is actually doing anything or just setting up the button and waiting for the javascript to actually do the execution.

<div class="contractLink"><button  type="button" value="upgrade to level 3"id="button533e24e4d7c79" class="green build" onclick="window.location.href = 'dorf1.php?a=16&amp;c=fb5ad7'; return false;">
    <div class="button-container addHoverClick">
        <div class="button-background">
            <div class="buttonStart">
                <div class="buttonEnd">
                    <div class="buttonMiddle"></div>
                </div>
            </div>
        </div>
        <div class="button-content">upgrade to level 3</div>
    </div>
</button>

This is where the script is that gets run when the button is clicked. I basically want to simulate the button being clicked through a python script which should go to a different URL and say "it's upgraded" (not literally say "it's upgraded" but make the website think it is and start the upgrading process)

<script type="text/javascript">
    window.addEvent('domready', function()
    {
    if($('button533e24e4d7c79'))
    {
        $('button533e24e4d7c79').addEvent('click', function ()
        {
            window.fireEvent('buttonClicked', [this, {"type":"button","value":"upgrade to level 3","name":"","id":"button533e24e4d7c79","class":"green build","title":"","confirm":"","onclick":"window.location.href = \u0027dorf1.php?a=16\u0026amp;c=fb5ad7\u0027; return false;"}]);
        });
    }
    });
</script>

Any help would be great, thanks for your time.

17
  • Does your solution have to be in python? Would you be open to using a solution in jquery? Commented Apr 4, 2014 at 3:36
  • I planned on making a program with a GUI that had options and would send these types of forms/requests etc. So in whatever language I can make that happen in is the one I'm going to use :) Commented Apr 4, 2014 at 3:40
  • Ok, I know how to easily do this in JQuery and using an HTML5-based GUI. I'm sure there's solutions in python too, with urllib or something, but I'm drawing a blank for that. Commented Apr 4, 2014 at 3:45
  • How easily would it be to make a GUI with JQuery? I planned on having this be a standalone from the browser is that still possible with JQuery or no? I really only want interaction with the website when I'm getting/sending requests. Commented Apr 4, 2014 at 3:47
  • If you develop the GUI with HTML5 and CSS styling, you can run in any browser that supports HTML5. I believe you can create standalone HTML5 apps (App.js I think will do the trick). Commented Apr 4, 2014 at 3:50

1 Answer 1

3

This is an alternative to using Python; this might be useful to those wishing to have a button click simulation without having normal subsequent action take place at the testers discretion.

Assuming that the id of the button to be simulated is "button533e24e4d7c79", the jQuery library has the trigger method to trigger the click (or any supported event for that matter) of this button:

$("#button533e24e4d7c79").trigger("click"); // This will mimic the clicking of the button (programmatically) -- requires Jquery

My understanding is that the trigger method will invoke the "click" event and stop at the event handler (i.e., won't follow links, won't continue with form submission, etc.). I haven't fully tested, but that's my understanding.

If it's desired to fully simulate the button click, jquery.simulate.js library (https://github.com/eduardolundgren/jquery-simulate/blob/master/jquery.simulate.js) provides the simulate method to allow following of links, etc.

The jQuery learning page at http://learn.jquery.com/events/triggering-event-handlers/ provides the proper explanation.

Sign up to request clarification or add additional context in comments.

1 Comment

Welp.. I can't even upvote because I don't have enough reputation... Someone upvote this guy! He was insanely helpful!

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.