Pressing A Webpage Button

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Elliot Temple

    Pressing A Webpage Button

    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
    Curiosity blog about philosophy and reason



    ---
    [This E-mail scanned for viruses by Declude Virus]

  • Brian Beck

    #2
    Re: Pressing A Webpage Button

    Elliot Temple wrote:[color=blue]
    > 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.[/color]

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

    --
    Brian Beck
    Adventurer of the First Order

    Comment

    • Steve M

      #3
      Re: Pressing A Webpage Button

      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:



      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.

      Comment

      • J Correia

        #4
        Re: Pressing A Webpage Button

        "Elliot Temple" <curi@curi.us > wrote in message
        news:mailman.33 5.1117645705.18 027.python-list@python.org ...[color=blue]
        > 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.)[/color]

        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("Inter netExplorer.App lication")
        ie.Visible = 1
        ie.Navigate("ht tp://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.clic k() # click on button 'btnG'


        Comment

        • Tim Roberts

          #5
          Re: Pressing A Webpage Button

          Elliot Temple <curi@curi.us > wrote:[color=blue]
          >
          >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.)[/color]

          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:



          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, timr@probo.com
          Providenza & Boekelheide, Inc.

          Comment

          • John J. Lee

            #6
            Re: Pressing A Webpage Button

            Elliot Temple <curi@curi.us > writes:
            [color=blue]
            > 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.[/color]
            [...]

            You might find the FAQ list and hints below useful after you get over
            the initial barriers (and the modules at the same site):






            John

            Comment

            • jerky

              #7
              Re: Pressing A Webpage Button

              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" <curi@curi.us >
              ??????:mailman. 335.1117645705. 18027.python-list@python.org ...[color=blue]
              > 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]
              >[/color]


              Comment

              • Grant Edwards

                #8
                Re: Pressing A Webpage Button

                On 2005-06-01, Elliot Temple <curi@curi.us > wrote:
                [color=blue]
                > How do I make Python press a button on a webpage?[/color]

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

                Guess what happens when you push that button: the browser
                opens a URL.
                [color=blue]
                > 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?[/color]

                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.
                [color=blue]
                > 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.)[/color]

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

                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="wh atever 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...

                Comment

                • Grant Edwards

                  #9
                  Re: Pressing A Webpage Button

                  On 2005-06-02, Grant Edwards <grante@visi.co m> wrote:[color=blue]
                  > On 2005-06-01, Elliot Temple <curi@curi.us > wrote:
                  >[color=green]
                  >> How do I make Python press a button on a webpage?[/color]
                  >
                  > You just do whatever action is specified for the form
                  > containing the button.
                  >[color=green]
                  >> I looked at urllib, but I only see how to open a URL with
                  >> that.[/color]
                  >
                  > Guess what happens when you push that button: the browser
                  > opens a URL.
                  >[color=green]
                  >> 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?[/color]
                  >
                  > 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.
                  >[color=green]
                  >> 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.)[/color]
                  >
                  > Sure. Just send back the field value in the normal manner
                  > using a GET.
                  >[color=green]
                  >> If someone could point me in the right direction it'd be appreciated.[/color]
                  >
                  > 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="wh atever 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?[/color]

                  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 --

                  Comment

                  • Paul Rubin

                    #10
                    Re: Pressing A Webpage Button

                    Grant Edwards <grante@visi.co m> writes:[color=blue][color=green]
                    > > I presume you can figure out how to open the URL instead of
                    > > printing it?[/color]
                    >
                    > Ah, never mind. That doesn't work. Google somehow detects
                    > you're not sending the query from a browser and bonks you.[/color]

                    Try setting the User-agent: header to one that looks like a browser.

                    Comment

                    • Esben Pedersen

                      #11
                      Re: Pressing A Webpage Button

                      J Correia wrote:[color=blue]
                      > "Elliot Temple" <curi@curi.us > wrote in message
                      > news:mailman.33 5.1117645705.18 027.python-list@python.org ...
                      >[color=green]
                      >>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.)[/color]
                      >
                      >
                      > 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("Inter netExplorer.App lication")
                      > ie.Visible = 1
                      > ie.Navigate("ht tp://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.clic k() # click on button 'btnG'[/color]

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

                      /Esben

                      Comment

                      • J Correia

                        #12
                        Re: Pressing A Webpage Button

                        "Esben Pedersen" <n.20.e5ke@spam gourmet.com> wrote in message
                        news:42a0a23b$0 $18648$14726298 @news.sunsite.d k...[color=blue]
                        > How do i know which methods the ie object has? dir(ie) doesn't show
                        > Navigate.[/color]

                        For ie object:
                        Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.


                        For document object:
                        Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.




                        Comment

                        • calfdog@yahoo.com

                          #13
                          Re: Pressing A Webpage Button

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

                          ie.Document.for ms['f'].elements['btnG'].FireEvent('onC lick')


                          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:




                          Elliot Temple wrote:[color=blue]
                          > 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][/color]

                          Comment

                          Working...