Get Chrome tab URL in Python
Don't worry about local-language solutions and support for Chrome, Firefox, Edge, Opera and other most chromium engine browsers:
Support to modify current tab url, other browsers can add their own adaptations if they are not available, and more functions supported by UIAutomation can be customized.
import uiautomation as auto
class BrowserWindow:
def __init__(self, browser_name, window_index=1):
"""
A Browser Window support UIAutomation.
:param browser_name: Browser name, support 'Google Chrome', 'Firefox', 'Edge', 'Opera', etc.
:param window_index: Count from back to front, default value 1 represents the most recently created window.
"""
if browser_name == 'Firefox':
addr_bar = auto.Control(Depth=1, ClassName='MozillaWindowClass', foundIndex=window_index) \
.ToolBarControl(AutomationId='nav-bar').ComboBoxControl(Depth=1, foundIndex=1) \
.EditControl(Depth=1, foundIndex=1)
else:
win = auto.Control(Depth=1, ClassName='Chrome_WidgetWin_1', SubName=browser_name, foundIndex=window_index)
win_pane = win.PaneControl(Depth=1, Compare=lambda control, _depth: control.Name != '')
if browser_name == 'Edge':
addr_pane = win_pane.PaneControl(Depth=1, foundIndex=1).PaneControl(Depth=1, foundIndex=2) \
.PaneControl(Depth=1, foundIndex=1).ToolBarControl(Depth=1, foundIndex=1)
elif browser_name == 'Opera':
addr_pane = win_pane.GroupControl(Depth=1, foundIndex=1).PaneControl(Depth=1, foundIndex=1) \
.PaneControl(Depth=1, foundIndex=2).GroupControl(Depth=1, foundIndex=1) \
.GroupControl(Depth=1, foundIndex=1).ToolBarControl(Depth=1, foundIndex=1) \
.EditControl(Depth=1, foundIndex=1)
else:
addr_pane = win_pane.PaneControl(Depth=1, foundIndex=2).PaneControl(Depth=1, foundIndex=1) \
.PaneControl(Depth=1, Compare=lambda control, _depth:
control.GetFirstChildControl() and control.GetFirstChildControl().ControlTypeName == 'ButtonControl')
addr_bar = addr_pane.GroupControl(Depth=1, foundIndex=1).EditControl(Depth=1)
assert addr_bar is not None
self.addr_bar = addr_bar
@property
def current_tab_url(self):
"""Get current tab url."""
return self.addr_bar.GetValuePattern().Value
@current_tab_url.setter
def current_tab_url(self, value: str):
"""Set current tab url."""
self.addr_bar.GetValuePattern().SetValue(value)
browser = BrowserWindow('Google Chrome')
print(browser.current_tab_url)
browser.current_tab_url = 'www.google.com'
print(browser.current_tab_url)
The principle behind pywinauto and uiautomation is both Windows UI Automation.
Pywinauto search control was too slow for me because it needed to search all the subtrees. If you want faster speed, customizing the search location to access the UI may be faster, uiautomation is a wrapper package Python-UIAutomation-for-Windows.
The above code test first acquisition speed is within 0.1s, average 0.05s, re-acquisition based on cache will be faster.
You can get the current URL (or at least what is typed in the address bar) via
from pywinauto import Application
app = Application(backend='uia')
app.connect(title_re=".*Chrome.*")
element_name="Address and search bar"
dlg = app.top_window()
url = dlg.child_window(title=element_name, control_type="Edit").get_value()
print(url)
If you use another language the element_name
might be different. You can get the corresponding name via inspect.exe
which is part of the Windows 10 SDK\Windows Kit under C:\Program Files (x86)\Windows Kits\10\bin\x64\inspect.exe
. Then just hover over the address bar and you'll find your name.
This by the way works with many browsers:
- Vivaldi:
Search or enter an address
- Firefox:
Search with Google or enter address
- Opera:
Address field
- Chrome:
Address and search bar
You may consider usink keys / hotkeys.
Selecting
F6 - acces your url for focused active tab
import pyautogui
pyautogui.click(0, 200) # a random click for focusing the browser
pyautogui.press('f6')
CTRL + TAB - next tab
pyautogui.hotkey('ctrl', 'tab')
CTRL + SHIFT + TAB - previous tab
pyautogui.hotkey('ctrl', 'shift', 'tab')
ALT + TAB - for another chrome window ( usefull if you know you are only chrome opened)
pyautogui.hotkey('alt', 'tab')
CTRL + T - open new tab
pyautogui.hotkey('ctrl', 't')
CTRL + W - close curent tab
pyautogui.hotkey('ctrl', 'w')
Copying
For copy the url, you can use either pyperclip...
pyautogui.hotkey('ctrl', 'c') # for copying the selected url
import pyperclip # pip install pyperclip required
url = pyperclip.paste()
print(url)
either clipboard module...
pyautogui.hotkey('ctrl', 'c') # for copying the selected url
import clipboard # pip install clipboard required
url = clipboard.paste()
print(url)
More about hotkeys into : documentation1, documentation2
PS: I have Chrome x78.0 on windows 64 bit os, and it's working for me. :)