manipulating gui elements on a web page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rdps
    New Member
    • Nov 2006
    • 13

    manipulating gui elements on a web page

    I am a newbie to the python world. I am trying my hands at one task:

    I am trying to access a website, say, "http://xyz.com" and then trying to maniulate the gui elememts already present on the page.

    I am able to open the site using:

    webbrowser.open ("http://xyz.com")

    But now, i have to enter some text in a text box and click on the button, which i am unaware how to do using python. Can anyone guide me through?

    TIA,
    rdps
    Last edited by rdps; Nov 8 '06, 11:09 AM. Reason: spelling mistake
  • fuffens
    New Member
    • Oct 2006
    • 38

    #2
    Here is an example of how to use win32com to open the google page and paste python into the text field.

    Code:
    from win32com.client import *
    import time
    ie = Dispatch("InternetExplorer.Application")
    ie.Visible = 1
    ie.Navigate("www.google.com")
    time.sleep(1)
    page = ie.Document
    page.activeElement.value = 'python'
    win32com is included in PythonWin (look at other posts from me). The sleep is a little lame. I am sure you can find a better way to check if the page is loaded.

    Best regards
    /Fredrik

    Comment

    • rdps
      New Member
      • Nov 2006
      • 13

      #3
      Thanks a lot indeed!!!

      Can one use cPAMIE module to do the same stuff ? i read about this this module in some of the other posting. Is looks simpler to use. Could there be some disadvantages of using the cPAMIE module? Please do let me know.

      Comment

      • fuffens
        New Member
        • Oct 2006
        • 38

        #4
        I have never used cPAMIE, but from what I can read about it, it seems to be an easier way to control the web browser than to use COM. It is also a test framework. So if your intentions are to test a web solution you should defently try cPAMIE. And it's free! Thank you rdps. I will also take a look at cPAMIE when I have the time.

        Best regards
        /Fredrik

        Comment

        • rdps
          New Member
          • Nov 2006
          • 13

          #5
          Hello,

          cPAMIE definetly looks like an easier way but i am facing problems in extracting elements out of the tables, where in the tags in the html source view dont mention any id for the table identification.

          All the functions related to the tables, ask for an id to be mentioned, which is unavailable to me. Do you see any solution?

          Comment

          • rdps
            New Member
            • Nov 2006
            • 13

            #6
            I would elaborate my prev mail a bit. Say, i am havaing a html code for a web page:

            <html>
            <body>
            <table>
            <tr>
            <td>"<a href="">abc"</a></td>
            <td>"xyz"</td>
            </tr>
            <tr>
            <td>"123"</td>
            <td>"456"</td>
            </tr>
            </table>
            </body>
            </html>


            now in all methods that PAMIE offers say, tableGetData (name) or tableExists (name) requires id, name or index as the parameter(name in this case).

            Now for te above code what is the "name" which i should pass as parameter? How do i extract data from the table?

            Regards,
            rdps

            Comment

            • fuffens
              New Member
              • Oct 2006
              • 38

              #7
              Hi rdps!

              I am not that great at html, but from what I can see there is no id or name on your table. There probably has to be some sort of identification in order to access the table. When using COM you can request to get all links in a Document for example and then access them as a list with index in brackets, [i]. I am sure you can find all tables in a similar way. I haven't looked at the documentation of PAMIE, but maybe you can do the same thing using PAMIE.

              BR
              /Fredrik

              Comment

              • fuffens
                New Member
                • Oct 2006
                • 38

                #8
                Here is how you would access a link with the previous COM example by retreiving a list of links

                Code:
                page.links[0].click()
                I will look at PAMIE later... don't have time right now. Please let me know if you find a way!

                Comment

                • rdps
                  New Member
                  • Nov 2006
                  • 13

                  #9
                  I tried using com to achieve my goal i.e to manipulate various ui components on a web page.

                  I dont know why, but this seemingly correct code gives following error :
                  -------------------------------------------------- Code---------------------------------------------------------
                  <b>
                  Code:
                  import win32com.client 
                  
                  ie = win32com.client.Dispatch("InternetExplorer.Application")
                  ie.Visible = 1
                  ie.navigate("http://google.com")
                  
                  page = ie.Document
                  links=page.links
                  links[0].click()
                  </b>

                  ----------------------------------------------------Error--------------------------------------------------------
                  Traceback (most recent call last):
                  File "C:\Eclipse\ecl ipse\workspace\ p\src\pkg\try_w ith_com.py", line 7, in ?
                  page = ie.Document
                  File "C:\Python24\Li b\site-packages\win32c om\client\dynam ic.py", line 491, in __getattr__
                  raise pythoncom.com_e rror, details
                  pywintypes.com_ error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147467259), None)
                  ---------------------------------------------------END----------------------------------------------------------
                  I am able to topen the browser but nothin happens after that. Could you please guide me where ami going wrong?
                  Last edited by bartonc; Dec 18 '06, 09:00 PM. Reason: added [code][/code] tags

                  Comment

                  • bartonc
                    Recognized Expert Expert
                    • Sep 2006
                    • 6478

                    #10
                    You need to wait for the app to get running:

                    Code:
                    import win32com.client
                    from time import sleep
                    ie = win32com.client.Dispatch("InternetExplorer.Application")
                    ie.Visible = 1
                    ie.navigate("[url="http://google.com"]http://google.com[/url]")
                    sleep(10)
                    page = ie.Document
                    links=page.links
                    links[0].click()
                    works

                    Comment

                    • rdps
                      New Member
                      • Nov 2006
                      • 13

                      #11
                      Thnaks a ton!!! This did the trick.

                      Comment

                      • bartonc
                        Recognized Expert Expert
                        • Sep 2006
                        • 6478

                        #12
                        Originally posted by rdps
                        Thnaks a ton!!! This did the trick.
                        You are welcome! Of course, you can play with the length of the delay a bit. If you have a really fast internet connection , it may be more like 1 second. If you want it to run on slower internet connections, use something like this:
                        my internet connection is 3 Meg
                        lowest setting is 2 seconds
                        slowest internet connection to run is 56KBS
                        so
                        3/.056 = 53.57
                        53.57 times 2 seconds = 107 seconds for everybody to be able to run your program.

                        Comment

                        • rdps
                          New Member
                          • Nov 2006
                          • 13

                          #13
                          Hey! thanks for the explanation.

                          What i want to do next is enter text in the text box and click 'Saerch'. Any pointers regarding how to do that?

                          The real prb is where the gui elements have no ids associated in the respective html doc.

                          Comment

                          • bartonc
                            Recognized Expert Expert
                            • Sep 2006
                            • 6478

                            #14
                            Originally posted by rdps
                            Hey! thanks for the explanation.

                            What i want to do next is enter text in the text box and click 'Saerch'. Any pointers regarding how to do that?

                            The real prb is where the gui elements have no ids associated in the respective html doc.
                            There are two experts on this forum who very good with web stuff.
                            fuffens and kudos are really smart.

                            fuffens says


                            Code:
                            [left]from win32com.client import *[/left]
                            import time
                            ie = Dispatch("InternetExplorer.Application")
                            ie.Visible = 1
                            ie.Navigate("www.google.com")
                            time.sleep(1)
                            page = ie.Document
                            page.activeElement.value = 'python'
                            [left][/left]

                            Look at posts in this forum with similar names to query. If you click on the name of one of these experts, you can then "find all posts by ..." to see all the answers that they have given to others.

                            Comment

                            • willitfw
                              New Member
                              • Dec 2006
                              • 1

                              #15
                              I am using PAMIE to access data from the internet. My question is how do I use PAMIE to obtain the functionality of the right mouse button? Essentially, I want to right click a link, then click on Save Target As...

                              Any guidance will be appreciated.

                              Comment

                              Working...