<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
    <title></title>
  </head>
  <body text="#000000" bgcolor="#ffffff">
    On 1:59 PM, He Jibo wrote:
    <blockquote
cite="mid:%3CAANLkTikJODB3f7cJzUAXjSPPm4eoVHNTQB9=AEcB==wW@mail.gmail.com%3E"
      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 moz-do-not-send="true"
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
          class="il">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 moz-do-not-send="true"
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 moz-do-not-send="true"
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 moz-do-not-send="true"
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 moz-do-not-send="true"
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>
      &nbsp; &nbsp;for win in shell.Windows():<br>
      # &nbsp; &nbsp; &nbsp; &nbsp;print win<br>
      &nbsp; &nbsp; &nbsp; &nbsp;if win.Name == "Windows Internet Explorer":<br>
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return win<br>
      &nbsp; &nbsp;return None<br>
      <br>
      def main():<br>
      &nbsp; &nbsp;<span class="il">ie</span> = get_ie(SHELL)<br>
      &nbsp; &nbsp;if <span class="il">ie</span>:<br>
      &nbsp; &nbsp; &nbsp; &nbsp;print <span class="il">ie</span>.LocationURL<br>
      &nbsp; &nbsp; &nbsp; &nbsp;print <span class="il">ie</span>.LocationName<br>
      &nbsp; &nbsp; &nbsp; &nbsp;print <span class="il">ie</span>.ReadyState<br>
      &nbsp; &nbsp; &nbsp; &nbsp;print <span class="il">ie</span><br>
      &nbsp; &nbsp; &nbsp; &nbsp;print <span class="il">ie</span>.Document.title<br>
      &nbsp; &nbsp; &nbsp; &nbsp;print <span class="il">ie</span>.Document.location<br>
      &nbsp; &nbsp; &nbsp; &nbsp;print <span class="il">ie</span>.Document.forms<br>
      <br>
      # &nbsp; &nbsp; &nbsp; &nbsp;title = win32gui.GetWindowText(<span class="il">ie</span>)<br>
      # &nbsp; &nbsp; &nbsp; &nbsp;print title<br>
      <br>
      &nbsp; &nbsp;else:<br>
      &nbsp; &nbsp; &nbsp; &nbsp;print "no <span class="il">ie</span> window"<br>
      <br>
      if __name__ == '__main__':<br>
      &nbsp; &nbsp;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 moz-do-not-send="true" href="http://www.hejibo.info">www.hejibo.info</a><br>
      <br>
    </blockquote>
    <br>
    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 class="moz-txt-link-freetext" href="http://www.rutherfurd.net/python/sendkeys/">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 class="moz-signature">-- <br>
      Mike Driscoll
      <br>
      Applications Specialist
      <br>
      Blog:&nbsp;&nbsp; <a class="moz-txt-link-freetext" href="http://blog.pythonlibrary.org">http://blog.pythonlibrary.org</a><br>
    </div>
  </body>
</html>