Thanks so much for your valuable information. I will only need the url of a browser when it is in the front. I am going to try SendKey, it seems to be a good way to go. Have a nice day!<br><br clear="all">---------------------------<br>
He Jibo<br>Department of Psychology,<br>Beckman Institute for Advanced Science and Technology<br>University of Illinois, Urbana Champaign,<br>603 East Daniel St.,<br>Champaign, IL 61820<br>website: <a href="http://www.hejibo.info">www.hejibo.info</a><br>
<br>
<br><br><div class="gmail_quote">On Tue, Nov 30, 2010 at 9:09 AM, Mike Driscoll <span dir="ltr"><<a href="mailto:mdriscoll@co.marshall.ia.us">mdriscoll@co.marshall.ia.us</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div text="#000000" bgcolor="#ffffff"><div><div></div><div class="h5">
On 1:59 PM, He Jibo wrote:
<blockquote type="cite">Hi,<br>
I am writing a small program, which needs to get the URL of the
active<br>
tab in either of firefox, internet exploerer or chrome.<br>
My need is similar as the one posted at,<br>
<a href="http://stackoverflow.com/questions/3631216/how-do-i-get-the-url-of-the-visible-tab-in-firefox-ie-chrome" target="_blank">http://stackoverflow.com/questions/3631216/how-do-i-get-the-url-of-the-visible-tab-in-firefox-<span>ie</span>-chrome</a><br>
<br>
I did a lot of Googling, and get the following code. The following<br>
code can get the url of the first tab in internet explorer. My<br>
question is, how can I get the url of the current active tab?
Thanks.<br>
<br>
'''<br>
<a href="http://efreedom.com/Question/1-2555905/Get-Internet-Explorer-Address-Bar-Python" target="_blank">http://efreedom.com/Question/1-2555905/Get-Internet-Explorer-Address-Bar-Python</a><br>
<a href="http://blogs.msdn.com/b/oldnewthing/archive/2005/07/05/435657.aspx" target="_blank">http://blogs.msdn.com/b/oldnewthing/archive/2005/07/05/435657.aspx</a><br>
<a href="http://mail.python.org/pipermail/python-win32/2004-June/002040.html" target="_blank">http://mail.python.org/pipermail/python-win32/2004-June/002040.html</a><br>
<a href="http://code.activestate.com/recipes/302324-browser-automation-tool-py-class-file/" target="_blank">http://code.activestate.com/recipes/302324-browser-automation-tool-py-class-file/</a><br>
'''<br>
from win32com.client import Dispatch<br>
import win32api, win32con,win32gui<br>
<br>
<br>
SHELL = Dispatch("Shell.Application")<br>
<br>
def get_ie(shell):<br>
for win in shell.Windows():<br>
# print win<br>
if win.Name == "Windows Internet Explorer":<br>
return win<br>
return None<br>
<br>
def main():<br>
<span>ie</span> = get_ie(SHELL)<br>
if <span>ie</span>:<br>
print <span>ie</span>.LocationURL<br>
print <span>ie</span>.LocationName<br>
print <span>ie</span>.ReadyState<br>
print <span>ie</span><br>
print <span>ie</span>.Document.title<br>
print <span>ie</span>.Document.location<br>
print <span>ie</span>.Document.forms<br>
<br>
# title = win32gui.GetWindowText(<span>ie</span>)<br>
# print title<br>
<br>
else:<br>
print "no <span>ie</span> window"<br>
<br>
if __name__ == '__main__':<br>
main()<br clear="all">
---------------------------<br>
He Jibo<br>
Department of Psychology,<br>
Beckman Institute for Advanced Science and Technology<br>
University of Illinois, Urbana Champaign,<br>
603 East Daniel St.,<br>
Champaign, IL 61820<br>
website: <a href="http://www.hejibo.info" target="_blank">www.hejibo.info</a><br>
<br>
</blockquote>
<br></div></div>
It will probably be frowned on here, but one of the easiest ways
would be to use SendKeys in combination with some Windows hackery. I
have attached some code I came up with after getting help from
several of the fine people on this list. It shows how to bring a
window into focus by just passing in an expected string. In this
case, you would want to use something like "Windows Internet
Explorer" and have it search for that.<br>
<br>
Once that's in focus, you can use the SendKeys package
(<a href="http://www.rutherfurd.net/python/sendkeys/" target="_blank">http://www.rutherfurd.net/python/sendkeys/</a>) to send an ALT+D to
select the url and then CTRL+C to copy it. Then you can use the
win32clipboard module from PyWin32 to grab the text from the
clipboard.<br>
<br>
It's a hack, but it's kind of fun. Of course, you can't use your PC
while the script runs or you might interrupt the process and send
the keys to the wrong window.<br>
<br>
<br>
<div>-- <br>
Mike Driscoll
<br>
Applications Specialist
<br>
Blog: <a href="http://blog.pythonlibrary.org" target="_blank">http://blog.pythonlibrary.org</a><br>
</div>
</div>
</blockquote></div><br>