2

I'm trying to use Pywinauto to get the Chrome tab's url like this:

(pseudo code)

  1. Press F6 to direct url line.
  2. Ctrl + C to copy url
  3. Get the url from scrapbook
  4. Deal with the url by BS4, Requests, selenium and etc.

I am done with step 1 and stuck at step 2. Again, I don't know how to deal with step 3. Thus, think the method I figure out is incorrect and not efficient. Can anybody suggest what I should do or give me a better way?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Mars Lee
  • 1,845
  • 5
  • 17
  • 37
  • It's not quite clear what you're trying to do... You want to copy url from already opened `Chrome` browser window to use it? Where you want to use it? – Andersson Feb 18 '16 at 07:50
  • 1
    `pywinauto 0.5.4` is not able to get the URL from Chrome without tricks like `TypeKeys` and clipboard grabbing. Coming `pywinauto 0.6.0` will be able to do that right way. – Vasily Ryabov Feb 18 '16 at 08:04
  • @Andersson Sorry for the unclear. I want to acquire the url and use BS4, Requests, selenium to process it. For example, I might gain the table in the website by the url. Hope it helps! Thank you! – Mars Lee Feb 18 '16 at 09:53
  • @Vasily Ryabov Are The tricks, TypeKeys, you mentioned same as the content of this website? http://stackoverflow.com/questions/12056590/how-to-press-ctrl Besides, what you mean is I can get the url on Chrome by pywinauto 0.6.0?! Thank you for your help! – Mars Lee Feb 18 '16 at 09:56
  • Correct. Just use `connect` instead of `start` in case Chrome is already running. – Vasily Ryabov Feb 18 '16 at 10:00
  • I would also recommend `TypeKeys("^c", set_foreground=False)` since URL is already focused after `{F6}` and focus may switch to main window. – Vasily Ryabov Feb 18 '16 at 10:02
  • @Vasily Ryabov Really thank you! I just find out the connect method in the official document of Pywinauto! But I meet a new problem: I use app.connect(path = r'Chrome Path') to locate the existed Chrome, but it raises WindowNotFoundError(). Is it because I open two different account Chrome? And I also found the document supplying three ways for me to locate existed Chrome, including process, path and handle. Can you tell me which one is the better locating way in this case? – Mars Lee Feb 18 '16 at 10:26
  • I think `Application().connect(title=u'How can I get a url from Chrome by Python? - Stack Overflow - Google Chrome')` is better because the title should be unique. – Vasily Ryabov Feb 18 '16 at 10:34
  • Does this answer your question? [Get Chrome tab URL in Python](https://stackoverflow.com/questions/52675506/get-chrome-tab-url-in-python) – skjerns Jan 26 '20 at 14:00

2 Answers2

0

If all you want is to paste what you have in the clipboard to a string you could use one of the packages pyperclip or clipboard, which both are pip-installable.

import pyperclip
print(pyperclip.paste())

#or equivalently...
import clipboard
print(clipboard.paste())

will give output 'http://stackoverflow.com/questions/35475103/how-can-i-get-a-url-from-chrome-by-python' (twice...) if I copy the url for this page.

M.T
  • 4,917
  • 4
  • 33
  • 52
0

Just to summarize all the comments in one answer...

pywinauto 0.5.4 is not able to get the URL from Chrome without tricks like TypeKeys and clipboard grabbing. Coming pywinauto 0.6.0 will be able to do that right way.

Just use connect instead of start in case Chrome is already running.

I would also recommend TypeKeys("^c", set_foreground=False) since the URL is already focused after {F6} and focus may switch to the main window.

Application().connect(title=u'How can I get a url from Chrome by Python? - Stack Overflow - Google Chrome', found_index=0) is also better because the title should be unique. In case there are 2 windows found_index=0 or 1 is useful.

To get the clipboard data:

from pywinauto import clipboard
print(clipboard.GetData())
Vasily Ryabov
  • 9,386
  • 6
  • 25
  • 78
  • Really thank you for your help! I have final problem. Your suggestion works well, but it raise pywinauto.findwindows.WindowAmbiguousError, which means it found more than two matched windows. However, I am pretty sure I only open one windows called "How can I get a url from Chrome by Python? - Stack Overflow - Google Chrome." Have you ever met this problem before?! Thank you again! – Mars Lee Feb 18 '16 at 10:57
  • Maybe `found_index=0` in addition to the title will help you (didn't test it but should work I believe). – Vasily Ryabov Feb 18 '16 at 11:01