467,921 Members | 1,234 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,921 developers. It's quick & easy.

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
Nov 8 '06 #1
  • viewed: 6113
Share:
15 Replies
Here is an example of how to use win32com to open the google page and paste python into the text field.

Expand|Select|Wrap|Line Numbers
  1. from win32com.client import *
  2. import time
  3. ie = Dispatch("InternetExplorer.Application")
  4. ie.Visible = 1
  5. ie.Navigate("www.google.com")
  6. time.sleep(1)
  7. page = ie.Document
  8. 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
Nov 8 '06 #2
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.
Nov 9 '06 #3
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
Nov 9 '06 #4
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?
Nov 9 '06 #5
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
Nov 9 '06 #6
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
Nov 9 '06 #7
Here is how you would access a link with the previous COM example by retreiving a list of links

Expand|Select|Wrap|Line Numbers
  1. 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!
Nov 9 '06 #8
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>
Expand|Select|Wrap|Line Numbers
  1. import win32com.client 
  2.  
  3. ie = win32com.client.Dispatch("InternetExplorer.Application")
  4. ie.Visible = 1
  5. ie.navigate("http://google.com")
  6.  
  7. page = ie.Document
  8. links=page.links
  9. links[0].click()
</b>

----------------------------------------------------Error--------------------------------------------------------
Traceback (most recent call last):
File "C:\Eclipse\eclipse\workspace\p\src\pkg\try_with_c om.py", line 7, in ?
page = ie.Document
File "C:\Python24\Lib\site-packages\win32com\client\dynamic.py", line 491, in __getattr__
raise pythoncom.com_error, 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?
Nov 10 '06 #9
bartonc
Expert 4TB
You need to wait for the app to get running:

Expand|Select|Wrap|Line Numbers
  1. import win32com.client
  2. from time import sleep
  3. ie = win32com.client.Dispatch("InternetExplorer.Application")
  4. ie.Visible = 1
  5. ie.navigate("http://google.com")
  6. sleep(10)
  7. page = ie.Document
  8. links=page.links
  9. links[0].click()
  10.  
works
Nov 10 '06 #10
Thnaks a ton!!! This did the trick.
Nov 10 '06 #11
bartonc
Expert 4TB
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.
Nov 10 '06 #12
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.
Nov 10 '06 #13
bartonc
Expert 4TB
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


Expand|Select|Wrap|Line Numbers
  1. 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'
  •  

  • 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.
    Nov 10 '06 #14
    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.
    Dec 18 '06 #15
    bartonc
    Expert 4TB
    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.
    My guess is that PAMIE lets you run IE from behind the sceens and any action in IE's GUI from the user is handled by IE itself.
    Jan 3 '07 #16

    Post your reply

    Sign in to post your reply or Sign up for a free account.

    Similar topics

    4 posts views Thread by Michael J. Astrauskas | last post: by
    1 post views Thread by john | last post: by
    2 posts views Thread by jingalls | last post: by
    1 post views Thread by Geir SÝrensen | last post: by
    24 posts views Thread by RyanTaylor | last post: by
    4 posts views Thread by Greg Scharlemann | last post: by
    By using this site, you agree to our Privacy Policy and Terms of Use.