UIA v2

Post your working scripts, libraries and tools.
buster25
Posts: 10
Joined: 18 Jan 2024, 01:15

Re: UIA v2

Post by buster25 » 22 Jan 2024, 06:14

UIAViewer v2

Window Info:

ID 43845196,
Location {x:1159, y:234, w:256, h: 202},
ClassNN WindowsForms10.Window.808.app.0.e62666_r60_ad1,
Process Experior.exe
PID12964

Propèrties:
Type 50011 (MenuItem)
LocalizedType:"elemento de menú"
Name:"Mostrar Panel de Búsqueda"
Value:""
AutomationId:""
Location:{x:1161, y:386, w:252, h: 24}
ClassName:""
FullDescription:""
HelpText:""
AccessKey:""
AcceleratorKey:""
HasKeyboardFocus:0
IsKeyboardFocusable:1
ItemType:""
ProcessId:12964
IsEnabled:1
IsPassword:0
IsOffscreen:0
FrameworkId:""
IsRequiredForForm:0
ItemStatus:""

buster25
Posts: 10
Joined: 18 Jan 2024, 01:15

Re: UIA v2

Post by buster25 » 22 Jan 2024, 06:17

Descolada wrote:
22 Jan 2024, 06:11
@buster25 as you can see the DumpAll doesn't contain an element named "Ajuste perfecto". Is it the DumpAll for npEl or for contextMenuEl?
In any case you somehow need to target the context menu window with ElementFromHandle. If "A" (or active window) didn't work, then perhaps you need to use contextMenuEl := UIA.ElementFromHandle(WinExist("ahk_exe Experior.exe",, "Asiento Inscripción: Inscripción")) instead.
is the dump of contextMenuEl :cry:

buster25
Posts: 10
Joined: 18 Jan 2024, 01:15

Re: UIA v2

Post by buster25 » 22 Jan 2024, 06:19

Descolada wrote:
22 Jan 2024, 06:11
@buster25 as you can see the DumpAll doesn't contain an element named "Ajuste perfecto". Is it the DumpAll for npEl or for contextMenuEl?
In any case you somehow need to target the context menu window with ElementFromHandle. If "A" (or active window) didn't work, then perhaps you need to use contextMenuEl := UIA.ElementFromHandle(WinExist("ahk_exe Experior.exe",, "Asiento Inscripción: Inscripción")) instead.
Succes ¡¡¡¡¡ thanks @Descolada

mora145
Posts: 57
Joined: 25 Jun 2022, 15:31

Re: UIA v2

Post by mora145 » 26 Jan 2024, 00:35

@Descolada
Hi man! You can help me with this?

I have:
try{
hello := pointer.FindElements({Name:"About this result", Type:"Button"})
}
if(hello.Length > 0){
for el in hello {
webPage := el.WalkTree("-1",{Type:"Link"})

Screenshot_77.png
Screenshot_77.png (18.2 KiB) Viewed 994 times
The hello element is the red box, I find it, but webPage, just return the "Translate this page" element. Is there a filter that checks if the previous element has children?

Descolada
Posts: 1141
Joined: 23 Dec 2021, 02:30

Re: UIA v2

Post by Descolada » 26 Jan 2024, 01:48

@mora145 there isn't, but you can check the element for children with if Element.Children.Length and continue walking the tree if needed. Or if you know that you only need to skip "About this result" then perhaps el.WalkTree("-1", {Type:"Link", not:{Name:"About this result"}}) or el.WalkTree("-2", {Type:"Link"})

mora145
Posts: 57
Joined: 25 Jun 2022, 15:31

Re: UIA v2

Post by mora145 » 26 Jan 2024, 02:03

Descolada wrote:
26 Jan 2024, 01:48
@mora145 there isn't, but you can check the element for children with if Element.Children.Length and continue walking the tree if needed. Or if you know that you only need to skip "About this result" then perhaps el.WalkTree("-1", {Type:"Link", not:{Name:"About this result"}}) or el.WalkTree("-2", {Type:"Link"})
el.WalkTree("-1", {Type:"Link", not:{Name:"Translate this page"}}) Really it can work for me, I was trying make el.WalkTree("-1",{Name:!"Translate this page", Type:"Link"}) :lol:

Ty @Descolada

buster25
Posts: 10
Joined: 18 Jan 2024, 01:15

Re: UIA v2

Post by buster25 » 01 Feb 2024, 02:49

Hello @Descolada, can you help me? Clicking on the "Zoom" button enables the "control deslizante", with a value of 144. I need to move that slider to another value, for example 124

This is my code:

Code: Select all

;#NoTrayIcon
#include C:\New Suite\FUSION 3.0 GREDOS\Lib\UIA.ahk
SetTitleMatchMode 1
WinActivate "Asiento Inscripción: Inscripción"

ZoomEl := UIA.ElementFromHandle(WinExist("ahk_exe Experior.exe",, "Asiento Inscripción: Inscripción"))
ZoomEl.WaitElement({Type:"Button", LocalizedType:"botón", Name:"Zoom: 144"})
ZoomEl_boton := ZoomEl.FindElement({Type:"Button", LocalizedType:"botón", Name:"Zoom: 144"}).Click()

ZoomEl.WaitElement({Type:"Slider", LocalizedType:"control deslizante", Name:"Control de Edición"})
ZoomEl_valor := ZoomEl.FindElement({Type:"Slider", LocalizedType:"control deslizante", Name:"Control de Edición"})
ZoomEl_valor.Value := "68"

ExitApp
Attachments
Slider.JPG
Slider.JPG (120.12 KiB) Viewed 861 times
BotonZoom.JPG
BotonZoom.JPG (105.47 KiB) Viewed 861 times

songdg
Posts: 575
Joined: 04 Oct 2017, 20:04

Re: UIA v2

Post by songdg » 01 Feb 2024, 02:51

A List hosts several ListItems, would it be possible to reveal the current ListItem in the List 's dump information?
Type: 50008 (List) LocalizedType: "列表"
1: Type: 50007 (ListItem) LocalizedType: "列表项目"
1,1: Type: 50020 (Text) Name: "1" LocalizedType: "文本"
2: Type: 50007 (ListItem) LocalizedType: "列表项目"
2,1: Type: 50020 (Text) Name: "2" LocalizedType: "文本"
3: Type: 50007 (ListItem) LocalizedType: "列表项目"
3,1: Type: 50020 (Text) Name: "3" LocalizedType: "文本"
4: Type: 50007 (ListItem) LocalizedType: "列表项目"
4,1: Type: 50020 (Text) Name: "4" LocalizedType: "文本"
5: Type: 50007 (ListItem) LocalizedType: "列表项目"
5,1: Type: 50020 (Text) Name: "5" LocalizedType: "文本"
6: Type: 50007 (ListItem) LocalizedType: "列表项目"
6,1: Type: 50020 (Text) Name: "6" LocalizedType: "文本"
7: Type: 50007 (ListItem) LocalizedType: "列表项目"
7,1: Type: 50020 (Text) Name: "" LocalizedType: "文本"
Attachments
listb.png
listb.png (36.46 KiB) Viewed 888 times
lista.png
lista.png (37.45 KiB) Viewed 888 times

elbitjusticiero
Posts: 78
Joined: 06 May 2017, 11:07

Re: UIA v2

Post by elbitjusticiero » 06 Feb 2024, 05:50

@Descolada do you know why several pages in a website, like this one, can't be found by UIA? I get this message:
Error: No matching window found

---- E:\pgm\AHK\func_v2.ahk
500: }
503: {
▶ 504: Return UIA.ElementFromHandle(WinGetTitle("A"))
505: }
---- E:\pgm\AHK\Lib\Misc.ahk
030: Range

The current thread will exit.
This happens sometimes with this and other news websites, but most pages work well.

Descolada
Posts: 1141
Joined: 23 Dec 2021, 02:30

Re: UIA v2

Post by Descolada » 06 Feb 2024, 05:59

@elbitjusticiero it probably depends on what your title match mode is set to in AHK, because WinGetTitle will return the full title for the active window, and then ElementFromHandle will internally call WinExist(window title) which appears to be failing. Just use UIA.ElementFromHandle("A") instead, or

Code: Select all

if hWnd := WinExist("A")
    return UIA.ElementFromHandle(hWnd)

elbitjusticiero
Posts: 78
Joined: 06 May 2017, 11:07

Re: UIA v2

Post by elbitjusticiero » 06 Feb 2024, 06:19

Got it, thank you! UIA.ElementFromHandle("A") solved the problem.

My match mode is RegEx and I see the title includes a question mark. Hmmm.

mora145
Posts: 57
Joined: 25 Jun 2022, 15:31

Re: UIA v2

Post by mora145 » 11 Feb 2024, 22:49

Hi @Descolada
How I can avoid the error: failed to find the browser!? I have a lot of this

Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

Re: UIA v2

Post by Spitzi » 16 Feb 2024, 05:16

@Descolada Hi.

I am using UIA to do stuff in an app, which removes the focus from the element it was before. I was thinking I could use UIA.GetFocusedElement() to find out the focused element and when done doing stuff, to give the focus back to the element that had it before.
But the method throws an error:
Error.png
Error.png (16.34 KiB) Viewed 492 times
.

What can i do?

Thanks and Greets Spitzi

Descolada
Posts: 1141
Joined: 23 Dec 2021, 02:30

Re: UIA v2

Post by Descolada » 16 Feb 2024, 10:02

@Spitzi the error message corresponds to UIA.GetFocusedElementBuildCache() or UIA.GetFocusedElement(cacheRequest), not UIA.GetFocusedElement(). Are you sure your cache request is valid?

reddyshyam
Posts: 38
Joined: 24 Jul 2023, 04:34

Re: UIA v2

Post by reddyshyam » 17 Feb 2024, 00:21

Hi @Descolada ,

Good day! Thank you for the awesome script! I use it a little and learning more each day as a noob. 🙂I have a question or two. Please answer at your convenience.

1. I want to select a tab on name but I have plenty with same name, want to select the latest one or last one opened. How do I go about it?
2. Is there a way to run a URL without bring it to focus or open in browser? I know there are alternatives like wget or curl but I want to use browser's session so would like to invoke the browser in non intrusive or invisible manner. Is this possible?

Descolada
Posts: 1141
Joined: 23 Dec 2021, 02:30

Re: UIA v2

Post by Descolada » 17 Feb 2024, 01:44

@reddyshyam
1. You can use the same search condition as with selecting a tab, but using index:-1 to get the last matching element. The exact code is browser-dependant, so if you want an example then UIA_Browser.ahk implements SelectTab and GetTabs (you could use cUIA.GetTabs("Tab name")[-1].Click() to select the last one).
2. I haven't needed that so I don't have an exact answer, but you could try hiding the browser when you launch the URL and make it visible afterwards. A more advanced version involves using SetWindowsHookEx with a dll that prevents window activation. The following example requires CBTProc_PreventActivate.dll (from the attached zip file) in the same folder as the script, and prevents Chrome from being activated when a new tab is launched:

Code: Select all

#Requires AutoHotkey v2

hWnd := WinExist("ahk_exe chrome.exe")
hLib := DllCall("LoadLibraryW", "str", A_ScriptDir "\CBTProc_PreventActivate.dll", "ptr")
pCBTProc := DllCall("GetProcAddress", "ptr", hLib, "astr", "CBTProc", "ptr")
threadId := DllCall("GetWindowThreadProcessId", "Ptr", hWnd, "Ptr", 0, "UInt")
hHook := DllCall("SetWindowsHookEx", "Int", WH_CBT := 5, "Ptr", pCBTProc, "Ptr", hLib, "UInt", threadId, "Ptr")
Run "chrome.exe www.google.com"
MsgBox "Chrome tab has been silently opened"
DllCall("UnhookWindowsHookEx", "Ptr", hHook)
Source code of the dll:

Code: Select all

// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"

extern "C" __declspec(dllexport) LRESULT CALLBACK CBTProc(
	_In_ int    nCode,
	_In_ WPARAM wParam,
	_In_ LPARAM lParam
)
{
	if (nCode < 0) return CallNextHookEx(nullptr, nCode, wParam, lParam);

	switch (nCode)
	{
	case HCBT_ACTIVATE:
		return 1; // Prevent
	}
	return CallNextHookEx(nullptr, nCode, wParam, lParam);
}

BOOL APIENTRY DllMain(HMODULE hModule,
	DWORD  ul_reason_for_call,
	LPVOID lpReserved
)
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}
Attachments
CBTProc_PreventActivate.zip
(4.59 KiB) Downloaded 146 times

reddyshyam
Posts: 38
Joined: 24 Jul 2023, 04:34

Re: UIA v2

Post by reddyshyam » 17 Feb 2024, 02:32

Hi @Descolada ,

Thanks for the prompt reply. 🙏 Solution to question 1 looks like working, thank you.

Regarding second question, I want to be able to select elements of the invisible browser call just like we do via UIA browser script. I use Brave browser, the idea is not to interrupt my browsing and still check something on a URL like updates on a webpage, etc... In the above DLL hook example, I do not see cUIA := UIA_Browser("ahk_exe brave.exe") call so asking this question. I would like to check element on that page as shown below on a invisible call.

cUIA.ElementExist({Type:"Text", Name:"FULFILLED", matchmode:"Substring"}

Will this be possible?

Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

Re: UIA v2

Post by Spitzi » 17 Feb 2024, 12:15

Hi @Descolada I was using

Code: Select all

focusedEl := UIA.GetFocusedElement(cacheRequest := 0)
The call without the cacheRequest does not generate an error:

Code: Select all

focusedEl := UIA.GetFocusedElement()
In my code, i want to scroll down on a window - which gives the focus to the scrollbar of that window. So I thought something like this migt work:

Code: Select all

focusedEl := UIA.GetFocusedElement()																		; idea was to get the focused element and after scrolling give the focus back
scrollbarEl := befundWinEl.FindElement({Type:"ScrollBar"})
scrollbarEl.SetValue(scrollbarEl.Maximum)																		; this will activate the scrollbar.
focusedEl.SetFocus()
But it does not work. Do you have a suggestion how to solve this? Is it possible to scroll the scrollbar without giving it the focus?

Greets and Thanks once more. Spitzi

Descolada
Posts: 1141
Joined: 23 Dec 2021, 02:30

Re: UIA v2

Post by Descolada » 17 Feb 2024, 12:40

@reddyshyam generally it's not possible, because Chromium-based apps (like Brave) require the window to be at least partially visible for UIAutomation to work. This means you can't use UIA to read contents from a non-selected tab, nor a minimized window, nor a window completely obscured by another windows. There are workarounds for the last case, eg hiding the window, bringing it to front, and then sending the window back again, but it isn't 100% reliable. I'd recommend going the Chrome.ahk or Rufaydium approach, as they can be used to automate the browser more reliably and in the background.

@Spitzi if the group element that owns the scrollbar (eg a listbox, treeview etc) implements the ScrollPattern (or perhaps the scrollbar itself implements it), then you can use its SetScrollPercent method to scroll. See Example11_ScrollPatternScrollItemPattern.ahk in the Examples folder. If it does, then it shouldn't necessarily focus the element (you can additionally try UIA.AutoSetFocus := False to disable UIA automatically focusing elements before doing actions).

Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

Re: UIA v2

Post by Spitzi » 18 Feb 2024, 06:11

@Descolada Thanks for the tip.

UIA.AutoSetFocus := false works nicely. The scrollbar does not get the focus while scrolling.

Ist it possible that a .Click() method or an .Invoke() does not go through when AutoSetFocus is set to false?

Greets Spitzi

Post Reply

Return to “Scripts and Functions (v2)”