In this question How can I get a url from Chrome by Python?, it was brought up that you could grab the url from python in pywinauto 0.6. How is it done?
Asked
Active
Viewed 2,739 times
3
-
Good question. Thanks for heads up. But need some time to prepare code snippet workable without typing keys and mouse clicks. Did you try something on your side? Already learnt the [Getting Started Guide](https://pywinauto.readthedocs.io/en/latest/getting_started.html)? – Vasily Ryabov Sep 11 '17 at 16:12
-
No, sorry. I used copy and clipboard. – Mike McHenry Sep 11 '17 at 23:01
-
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:03
1 Answers
3
Using inspect.exe (which is mentioned in Getting Started) you can find Chrome's address bar element, and that its parameter "value" contains the current url.
I found two ways to get this url:
from __future__ import print_function
from pywinauto import Desktop
chrome_window = Desktop(backend="uia").window(class_name_re='Chrome')
address_bar_wrapper = chrome_window['Google Chrome'].main.Edit.wrapper_object()
Here's the first way:
url_1 = address_bar_wrapper.legacy_properties()['Value']
Here's the second:
url_2 = address_bar_wrapper.iface_value.CurrentValue
print(url_1)
print(url_2)
Also if protocol is "http" Chrome removes "http://" prefix. U can add sth like:
def format_url(url):
if url and not url.startswith("https://"):
return "http://" + url
return url