[英]Get Chrome tab URL in Python
我想獲取有關我的 Chrome 選項卡的信息,例如當前選項卡的 URL 或自動獲取所有 URL,但我找不到任何有關它的文檔。 我安裝了 Chrome API,但我所看到的沒有類似的東西。 謝謝你的幫助
不用擔心本地語言解決方案和對 Chrome、Firefox、Edge、Opera 和其他大多數 Chrome 引擎瀏覽器的支持:
支持修改當前tab url,其他瀏覽器沒有的可以自己添加適配,更多UIAutomation支持的功能可以自定義。
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)
pywinauto 和 uiautomation 背后的原理都是Windows UI Automation 。
Pywinauto 搜索控制對我來說太慢了,因為它需要搜索所有子樹。 如果你想要更快的速度,自定義搜索位置來訪問 UI 可能會更快, uiautomation 是一個包裝包Python- UIAutomation -for-Windows 。
上面代碼測試第一次獲取速度在0.1s以內,平均0.05s,基於緩存重新獲取會更快。
您可以考慮使用 usink鍵/熱鍵。
F6 - 訪問您的 url 以獲得焦點活動選項卡
import pyautogui
pyautogui.click(0, 200) # a random click for focusing the browser
pyautogui.press('f6')
CTRL + TAB - 下一個標簽
pyautogui.hotkey('ctrl', 'tab')
CTRL + SHIFT + TAB - 上一個選項卡
pyautogui.hotkey('ctrl', 'shift', 'tab')
ALT + TAB - 用於另一個 chrome 窗口(如果您知道只打開了 chrome,則很有用)
pyautogui.hotkey('alt', 'tab')
CTRL + T - 打開新標簽
pyautogui.hotkey('ctrl', 't')
CTRL + W - 關閉當前選項卡
pyautogui.hotkey('ctrl', 'w')
對於復制 url ,您可以使用 pyperclip ...
pyautogui.hotkey('ctrl', 'c') # for copying the selected url
import pyperclip # pip install pyperclip required
url = pyperclip.paste()
print(url)
無論是剪貼板模塊...
pyautogui.hotkey('ctrl', 'c') # for copying the selected url
import clipboard # pip install clipboard required
url = clipboard.paste()
print(url)
PS:我在 Windows 64 位操作系統上有 Chrome x78.0,它對我有用。 :)
您可以通過以下方式獲取當前 URL(或至少在地址欄中鍵入的內容)
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)
如果您使用另一種語言,則element_name
可能會有所不同。 您可以通過inspect.exe
獲取相應的名稱,它是C:\\Program Files (x86)\\Windows Kits\\10\\bin\\x64\\inspect.exe
下 Windows 10 SDK\\Windows Kit 的C:\\Program Files (x86)\\Windows Kits\\10\\bin\\x64\\inspect.exe
。 然后只需將鼠標懸停在地址欄上,您就會找到您的姓名。
順便說一下,這適用於許多瀏覽器:
Search or enter an address
Search with Google or enter address
Address field
Address and search bar
有人建議使用pyautogui進行復制/粘貼,但是我不想與硬件進行交互,像我之前說的那樣更自動化的東西就是我想要的東西
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.