尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

uiautomation-new 操作浏览器

uiautomation-new 操作浏览器 uiautomation-new是对uiautomation进行优化改进新增功能配合微软的Accessibility(Accessibility Insights Downloads)使用import uiautomation as autopip install uiautomation-new1. 获取所有Tab页面及切换Tab通过uiautomation-new可以枚举浏览器所有标签页并实现切换。需要定位浏览器窗口和Tab控件树结构。# 代码示例获取Chrome所有Tab标题并切换至指定Tab def tab_FindAll(): # window auto.WindowControl(ClassNameChrome_WidgetWin_1, RegexName.*Google Chrome) tabs window.FindAll(auto.ControlType.TabItemControl, maxDepth10) for tab in tabs: url tab.GetTabUrl(windowwindow) print(tab.Name, url) # 获取所有的tab页面url # tab_urls auto.GetAllTabUrls(window) tab_urls window.GetAllTabUrls() print(tabs:, tabs) print(tab_urls:, tab_urls) # 获取当前tab页对象 current_tab window.GetCurrentTab() print(current_tab) # 切换tab页面 # window.SwitchToTabByName(PyPI -) # 切换到名包含’PyPI -‘tab页面 window.SwitchToTabByUrl(curlconverter.com) # 切换url包含 curlconverter.com 的tab页面 url window.GetCurrentUrl() print(url)2. 操作地址栏并输入URL搜索控制浏览器地址栏输入URL需定位地址栏控件模拟键盘输入并触发回车事件。# 代码示例在地址栏输入URL并跳转 def hotkey_Helper(): window auto.WindowControl(ClassNameChrome_WidgetWin_1, RegexName.*Google Chrome) # 使浏览器界面聚焦 window.SetFocus() # 打卡新tab页面 window.Press_Hotkey(ctrl, t) control window.EditControl(RegexName地址和搜索栏) control.Press_Hotkey(ctrl, a) control.Press_Hotkey(backspace) # control.SendKeys(https://curlconverter.com/) url https://curlconverter.com/ auto.SetClipboardText(url) control.Press_Hotkey(ctrl, v) control.Press_Hotkey(enter)3. 使用OCR查找文本坐标结合OCR库如paddleocr识别屏幕文本获取目标坐标后通过uiautomation点击。# 代码示例OCR识别页面文本并返回坐标 def text_click(): 使用文本进行查找点击 使用 rapidocr 识别定位 :return: window auto.WindowControl(ClassNameChrome_WidgetWin_1, RegexName.*Google Chrome) control window.DocumentControl(Name搜索 - Microsoft 必应) # 控件截图 control.Screenshot(./control.png, offscreenTrue) # 根据坐标截图 # bitmap control.ToBitmap(x, y, width, height) # toBytes_type bytes(bitmap.ToBytes(png)) match auto.OCRClickText( # 需要查找坐标的目标文本 国际版, # 传具体的控件缩小范围提升查找速度 control, # 置信度越接近1越严格 minScore0.8, # 如果控件/窗口被其他顶层窗口遮挡 offscreenTrue, # SafeClick 开启由Click进行兜底点击 safeClickArgs{ fallbackToClick: True, }, ) print(match)4.点击页面下拉框定位下拉框控件需分析控件类型如ComboBox模拟展开选项并选择目标项。# 代码示例展开下拉框并选择指定选项 def click_combox(): 点击下拉框 :return: window auto.WindowControl(ClassNameChrome_WidgetWin_1, RegexName.*Google Chrome) combox window.ComboBoxControl(Name主要负责人) # 展开下拉框 expand combox.GetExpandCollapsePattern() if expand: expand.Expand() time.sleep(0.3) else: combox.Click() item auto.ListItemControl(searchFromControlcombox, Namelw) if item.Exists(1): # 优先用 SelectionItemPattern sip item.GetSelectionItemPattern() if sip: sip.Select() else: item.Click() # 兜底直接点 print(已切换) else: print(没找到对应 ListItem请用 Inspect 看弹出后的控件树)5.间接执行JS获取网页源码通过浏览器开发者工具协议如CDP注入JS代码获取DOM内容。# 代码示例通过CDP协议执行JS并返回源码 def get_page_html(): 获取浏览器页面源码 :return: window auto.WindowControl(ClassNameChrome_WidgetWin_1, RegexName.*Google Chrome) # 确保浏览器窗口获得焦点 window.SetFocus() # 刷新页面 window.Press_Hotkey(ctrl, r) console window.EditControl(RegexNameConsole prompt) if not console.Exists(1): window.Press_Hotkey(ctrl, shift, i) # console.SendKeys(console.log(hello world)) js_code copy(document.documentElement.outerHTML) auto.SetClipboardText(js_code) console.Press_Hotkey(ctrl, v) console.Press_Hotkey(enter) time.sleep(1.5) # window.Press_Hotkey(ctrl, shift, i) html_source auto.GetClipboardText() print(html_source)以上方法均基于uiautomation-new的控件树遍历和输入模拟能力实际使用时需根据浏览器版本和页面结构调整定位逻辑。
返回列表