I'm trying to use Pywinauto
to get the Chrome tab's url
like this:
(pseudo code)
- Press F6 to direct url line.
- Ctrl + C to copy url
- Get the url from scrapbook
- 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?
If all you want is to paste what you have in the clipboard to a string you could use one of the packages
pyperclip
orclipboard
, which both are pip-installable.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.Just to summarize all the comments in one answer...
pywinauto 0.5.4
is not able to get the URL from Chrome without tricks likeTypeKeys
and clipboard grabbing. Comingpywinauto 0.6.0
will be able to do that right way.Just use
connect
instead ofstart
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 windowsfound_index=0
or1
is useful.To get the clipboard data: