Pressing A Webpage Button

Discussion in 'Python' started by Elliot Temple, Jun 1, 2005.

  1. How do I make Python press a button on a webpage? I looked at
    urllib, but I only see how to open a URL with that. I searched
    google but no luck.

    For example, google has a button <input type=submit value="Google
    Search" name=btnG> how would i make a script to press that button?

    Just for fun, is there any way to do the equivalent of typing into a
    text field like the google search field before hitting the button?
    (I don't actually need to do this.)

    If someone could point me in the right direction it'd be appreciated.

    -- Elliot Temple
    http://www.curi.us/


    ---
    [This E-mail scanned for viruses by Declude Virus]
    Elliot Temple, Jun 1, 2005
    #1
    1. Advertising

  2. Elliot Temple

    Brian Beck Guest

    Elliot Temple wrote:
    > How do I make Python press a button on a webpage? I looked at
    > urllib, but I only see how to open a URL with that. I searched
    > google but no luck.


    Check out mechanize: http://wwwsearch.sourceforge.net/mechanize/

    --
    Brian Beck
    Adventurer of the First Order
    Brian Beck, Jun 1, 2005
    #2
    1. Advertising

  3. Elliot Temple

    Steve M Guest

    Do you actually need to 'press' the button? Or do you just need the
    effect that pressing the button would bring about (e.g., submitting a
    Google search query and receiving the results page)?

    If the latter, then you might want to search for, e.g., "html form get
    post" and check out some results. Pushing the button is often just
    loading a URL with parameters.

    For example, go to Google and type "html form get post" into the search
    box and press Submit. Now look at the URL you are visiting in your
    location bar, the URL of the search results. It will be something like:

    http://www.google.com/search?hl=en&q=html form get post&btnG=Google Search

    If you were to load that URL directly (without having gone to the
    Google homepage, typed "html form get post" in the text entry box and
    pressed submit) the exact same effect would happen. Filling in the box
    and clicking the submit button is just the user-friendly way of
    constructing that URL.
    Steve M, Jun 1, 2005
    #3
  4. Elliot Temple

    J Correia Guest

    "Elliot Temple" <> wrote in message
    news:...
    > How do I make Python press a button on a webpage? I looked at
    > urllib, but I only see how to open a URL with that. I searched
    > google but no luck.
    >
    > For example, google has a button <input type=submit value="Google
    > Search" name=btnG> how would i make a script to press that button?
    >
    > Just for fun, is there any way to do the equivalent of typing into a
    > text field like the google search field before hitting the button?
    > (I don't actually need to do this.)


    You don't say which OS... if you're running IE on Windows you
    can use COM as follows...

    from win32com.client import Dispatch
    from time import sleep

    ie = Dispatch("InternetExplorer.Application")
    ie.Visible = 1
    ie.Navigate("http://www.google.com")
    while ie.ReadyState != 4: # Wait for browser to finish loading.
    sleep(1)
    doc = ie.Document
    doc.f.q.value = "qwerty" # form name is 'f'; search field name is 'q'
    doc.f.btnG.click() # click on button 'btnG'
    J Correia, Jun 1, 2005
    #4
  5. Elliot Temple

    Tim Roberts Guest

    Elliot Temple <> wrote:
    >
    >How do I make Python press a button on a webpage? I looked at
    >urllib, but I only see how to open a URL with that. I searched
    >google but no luck.
    >
    >For example, google has a button <input type=submit value="Google
    >Search" name=btnG> how would i make a script to press that button?
    >
    >Just for fun, is there any way to do the equivalent of typing into a
    >text field like the google search field before hitting the button?
    >(I don't actually need to do this.)


    Both things are done the same way. The Google Search button is a field
    named "btnG" with the value "Google Search". The query field itself is
    named "q". Then, if you look at the HTML, you'll see that this is wrapped
    in a <form> with the action "/search".

    So, all you need to do, then, is to encode all this in a URL:

    http://www.google.com/search?btnG=Google Search&q=python urllib

    This only works because the Google "search" page accepts parameters using
    the "GET" method, which is what you get when you send parameters in the
    URL. Many forms only accept parameters using the "POST" method, in which
    you send the encoded parameters as the body of the HTTP request.

    You probably need to do some reading on HTTP, and the GET and POST methods
    of transmitting parameters.
    --
    - Tim Roberts,
    Providenza & Boekelheide, Inc.
    Tim Roberts, Jun 1, 2005
    #5
  6. Elliot Temple

    John J. Lee Guest

    John J. Lee, Jun 1, 2005
    #6
  7. Elliot Temple

    jerky Guest

    hi,
    urllib does not work when search from google.com,
    since google.com have some limitations to developer ,
    you can get more details from www.google.com/apis/

    "Elliot Temple" <>
    ??????:...
    > How do I make Python press a button on a webpage? I looked at urllib,
    > but I only see how to open a URL with that. I searched google but no
    > luck.
    >
    > For example, google has a button <input type=submit value="Google
    > Search" name=btnG> how would i make a script to press that button?
    >
    > Just for fun, is there any way to do the equivalent of typing into a text
    > field like the google search field before hitting the button? (I don't
    > actually need to do this.)
    >
    > If someone could point me in the right direction it'd be appreciated.
    >
    > -- Elliot Temple
    > http://www.curi.us/
    >
    >
    > ---
    > [This E-mail scanned for viruses by Declude Virus]
    >
    jerky, Jun 2, 2005
    #7
  8. On 2005-06-01, Elliot Temple <> wrote:

    > How do I make Python press a button on a webpage?


    You just do whatever action is specified for the form
    containing the button.

    > I looked at urllib, but I only see how to open a URL with
    > that.


    Guess what happens when you push that button: the browser
    opens a URL.

    > I searched google but no luck.
    >
    > For example, google has a button <input type=submit value="Google
    > Search" name=btnG> how would i make a script to press that button?


    Find the <form> containing the button, and look to see what the
    URL is specified. For Google, it looks something like this:

    <form action="/search" naem="f">

    So, /search is the URL you open.

    > Just for fun, is there any way to do the equivalent of typing
    > into a text field like the google search field before hitting
    > the button? (I don't actually need to do this.)


    Sure. Just send back the field value in the normal manner
    using a GET.

    > If someone could point me in the right direction it'd be appreciated.


    You need an introductory book on HTTP and HTML.

    If all you care about is a google query here's a python program
    that prints the URL you need to open for a google query:

    #!/usr/bin/python
    import urllib,sys,os
    queryString="whatever you're searching for"
    print 'http://www.google.com/search?'+urllib.urlencode({'q':queryString})

    I presume you can figure out how to open the URL instead of
    printing it?

    --
    Grant Edwards grante Yow! I'm in ATLANTIC CITY
    at riding in a comfortable
    visi.com ROLLING CHAIR...
    Grant Edwards, Jun 2, 2005
    #8
  9. On 2005-06-02, Grant Edwards <> wrote:
    > On 2005-06-01, Elliot Temple <> wrote:
    >
    >> How do I make Python press a button on a webpage?

    >
    > You just do whatever action is specified for the form
    > containing the button.
    >
    >> I looked at urllib, but I only see how to open a URL with
    >> that.

    >
    > Guess what happens when you push that button: the browser
    > opens a URL.
    >
    >> I searched google but no luck.
    >>
    >> For example, google has a button <input type=submit value="Google
    >> Search" name=btnG> how would i make a script to press that button?

    >
    > Find the <form> containing the button, and look to see what the
    > URL is specified. For Google, it looks something like this:
    >
    ><form action="/search" naem="f">
    >
    > So, /search is the URL you open.
    >
    >> Just for fun, is there any way to do the equivalent of typing
    >> into a text field like the google search field before hitting
    >> the button? (I don't actually need to do this.)

    >
    > Sure. Just send back the field value in the normal manner
    > using a GET.
    >
    >> If someone could point me in the right direction it'd be appreciated.

    >
    > You need an introductory book on HTTP and HTML.
    >
    > If all you care about is a google query here's a python program
    > that prints the URL you need to open for a google query:
    >
    > #!/usr/bin/python
    > import urllib,sys,os
    > queryString="whatever you're searching for"
    > print 'http://www.google.com/search?'+urllib.urlencode({'q':queryString})
    >
    > I presume you can figure out how to open the URL instead of
    > printing it?


    Ah, never mind. That doesn't work. Google somehow detects
    you're not sending the query from a browser and bonks you.

    --
    Grant Edwards grante Yow! ... I'm IMAGINING a
    at sensuous GIRAFFE, CAVORTING
    visi.com in the BACK ROOMof a KOSHER
    DELI --
    Grant Edwards, Jun 2, 2005
    #9
  10. Elliot Temple

    Paul Rubin Guest

    Grant Edwards <> writes:
    > > I presume you can figure out how to open the URL instead of
    > > printing it?

    >
    > Ah, never mind. That doesn't work. Google somehow detects
    > you're not sending the query from a browser and bonks you.


    Try setting the User-agent: header to one that looks like a browser.
    Paul Rubin, Jun 2, 2005
    #10
  11. J Correia wrote:
    > "Elliot Temple" <> wrote in message
    > news:...
    >
    >>How do I make Python press a button on a webpage? I looked at
    >>urllib, but I only see how to open a URL with that. I searched
    >>google but no luck.
    >>
    >>For example, google has a button <input type=submit value="Google
    >>Search" name=btnG> how would i make a script to press that button?
    >>
    >>Just for fun, is there any way to do the equivalent of typing into a
    >>text field like the google search field before hitting the button?
    >>(I don't actually need to do this.)

    >
    >
    > You don't say which OS... if you're running IE on Windows you
    > can use COM as follows...
    >
    > from win32com.client import Dispatch
    > from time import sleep
    >
    > ie = Dispatch("InternetExplorer.Application")
    > ie.Visible = 1
    > ie.Navigate("http://www.google.com")
    > while ie.ReadyState != 4: # Wait for browser to finish loading.
    > sleep(1)
    > doc = ie.Document
    > doc.f.q.value = "qwerty" # form name is 'f'; search field name is 'q'
    > doc.f.btnG.click() # click on button 'btnG'


    How do i know which methods the ie object has? dir(ie) doesn't show
    Navigate.

    /Esben
    Esben Pedersen, Jun 3, 2005
    #11
  12. Elliot Temple

    J Correia Guest

    J Correia, Jun 3, 2005
    #12
  13. Elliot Temple

    Guest

    You also may want to fire an event when a button is pressed such as:

    ie.Document.forms['f'].elements['btnG'].FireEvent('onClick')


    Events:
    onclick
    Fires when the user clicks the left mouse button on the object.

    onsubmit
    Fires when a FORM is about to be submitted.

    For more go here:
    http://msdn.microsoft.com/library/d...hor/dhtml/reference/dhtml_reference_entry.asp



    Elliot Temple wrote:
    > How do I make Python press a button on a webpage? I looked at
    > urllib, but I only see how to open a URL with that. I searched
    > google but no luck.
    >
    > For example, google has a button <input type=submit value="Google
    > Search" name=btnG> how would i make a script to press that button?
    >
    > Just for fun, is there any way to do the equivalent of typing into a
    > text field like the google search field before hitting the button?
    > (I don't actually need to do this.)
    >
    > If someone could point me in the right direction it'd be appreciated.
    >
    > -- Elliot Temple
    > http://www.curi.us/
    >
    >
    > ---
    > [This E-mail scanned for viruses by Declude Virus]
    , Jun 23, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philip Townsend

    button focus and pressing enter

    Philip Townsend, Aug 14, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    8,757
  2. Philip Townsend

    button focus and pressing enter

    Philip Townsend, Aug 25, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    2,243
    Philip Townsend
    Aug 25, 2003
  3. Dan Brussee

    Default button - pressing Enter

    Dan Brussee, Feb 19, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    459
  4. Replies:
    15
    Views:
    960
    Mike Meyer
    Nov 9, 2005
  5. albert kao
    Replies:
    1
    Views:
    2,011
    marisa lee
    Apr 10, 2010
Loading...

Share This Page