How to Press a Webpage Button with Python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kttr
    New Member
    • Nov 2012
    • 12

    How to Press a Webpage Button with Python

    I would like to "press" a button on a webpage using a python script. When I say "press", I mean I would like to initiate all of the actions that would occur if you were to press the button with a mouse. Here is the html code for the button:

    Code:
     <input type="button" 
    name="ctl00$MainPlaceHolder$submitOrder" 
    value="Submit Order >>" 
    onclick="this.disabled = true; this.value = 'Submitting Order...';__doPostBack('ctl00$MainPlaceHolder$submitOrder','')" 
    id="ctl00_MainPlaceHolder_submitOrder" 
    style="width:150px;font: bold 12px arial" />
    Any help would be greatly appreciated.
  • kttr
    New Member
    • Nov 2012
    • 12

    #2
    EDIT:
    There is a website (called investopedia) that allows you to conduct stock market trades without actually spending money. It is a stock simulator. Here is what I would like to do:
    1. Log in to Investopedia
    2. Make a trade (select 100 shares of APPL)
    3. Execute the trade.
    I can do the first 2 using mechanize or selenium. However, the third part messes me up. In order to execute the trade, you must press the "submit order button". When you press the submit order button, you are redirected to another page which tells you your order has been submitted. From what I gather, here is what happens when I press "submit order":
    1. The order information is submitted to a server (or something like that).
    2. The order is assigned an ID number.
    3. Once the order has been submitted, a new url is created with the order ID in it, and you are sent to that url. (That would be the "your order has now been submitted" page).
    How would I re-create this process with python? Please be specific; the html code for the button is above.

    Comment

    • zmbd
      Recognized Expert Moderator Expert
      • Mar 2012
      • 5501

      #3
      This is not a code writeing/do your project service.
      You have your basic outline in #2 now you just need to research how it's done... more than likely a link into a dabase somehow...

      Post your code and I'm sure that someone will be able to help you thru this.

      Comment

      • kttr
        New Member
        • Nov 2012
        • 12

        #4
        Originally posted by zmbd
        This is not a code writeing/do your project service.
        You have your basic outline in #2 now you just need to research how it's done... more than likely a link into a dabase somehow...

        Post your code and I'm sure that someone will be able to help you thru this.

        Oh. whoops! I'm new to bytes. I already have some code, I guess I'm just being a little lazy. I'll post the python code I already have shortly; thanks for letting me know.

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          Not an issue.
          I figured you had the code to work with otherwise I would have closed your thread. Please remember to use the <CODE/> button to format your code when you post it.

          Comment

          • kttr
            New Member
            • Nov 2012
            • 12

            #6
            Originally posted by zmbd
            Not an issue.
            I figured you had the code to work with otherwise I would have closed your thread. Please remember to use the <CODE/> button to format your code when you post it.
            Ah, well. I figured it out on my own. But thanks for the help.

            Comment

            • zmbd
              Recognized Expert Moderator Expert
              • Mar 2012
              • 5501

              #7
              Would you please go ahead and post your solution so that others might benefit from it too - otherwise I should have simply deleted your thread to start with.

              Comment

              • kttr
                New Member
                • Nov 2012
                • 12

                #8
                Originally posted by zmbd
                Would you please go ahead and post your solution so that others might benefit from it too - otherwise I should have simply deleted your thread to start with.
                I understand. Here is the code, for anyone who might find it helpful:

                Code:
                from selenium import webdriver
                from selenium.webdriver.common.keys import Keys
                
                #find the id of the button
                
                find_it = browser.find_element_by_id("ctl00_MainPlaceHolder_submitOrder")
                
                #"press" the enter key (with selenium)
                
                find_it.send_keys(Keys.RETURN)
                
                #the order should be submitted.

                Comment

                Working...