UIA v2

Post your working scripts, libraries and tools.
Descolada
Posts: 1431
Joined: 23 Dec 2021, 02:30

Re: UIA v2

24 Apr 2024, 00:38

@shipaddicted unfortunately I don't have access to Salesforce so I can't debug this myself. It sounds like either a wrong element is found inside the switch statement, or it is a timing issue.

My first suggestion is to add some Highlights to see whether everything lights up correctly:

Code: Select all

	;'open' 2nd combobox & select option based on value of 1st combobox 
	y.WaitElement({Type:"ComboBox", Name:"Case Closed Reason",cs:0,mm:2}).Highlight().Invoke() 
	Switch statuss
	{
		case "Stage 1": y.WaitElement({Type:"ListItem", Name:"Paid Stage 1"}).Highlight().Invoke() 
		case "Stage 2": y.WaitElement({Type:"ListItem", Name:"Paid Stage 2"}).Highlight().Invoke() 
		case "Stage 3": y.WaitElement({Type:"ListItem", Name:"Paid Stage 3"}).Highlight().Invoke() 
		case "Stage 4": y.WaitElement({Type:"ListItem", Name:"Paid Stage 4"}).Highlight().Invoke() 
		case "Stage 5": y.WaitElement({Type:"ListItem", Name:"Paid Stage 5"}).Highlight().Invoke() 
		case "Stage 6": y.WaitElement({Type:"ListItem", Name:"Paid Stage 6"}).Highlight().Invoke() 
	}
	MsgBox "done"
If everything is highlighted correctly and the list element is still fake-clicked, then you could try replacing Invoke() with ControlClick() or Click("left") to see whether that makes a difference. Another possibility would be to run that part of the code twice, because you mentioned it works perfectly on the second try (perhaps for some reason the element isn't selected correctly on the first try?).

Also, are you sure that Invoke() is correct there? Comboboxes often have Expand() available, and ListItems may have Select(). They may have different effects than Invoke().

Fyi, I don't recommend UIA for browser automation. It is much more reliable to use Chrome.ahk, Rufaydium, or write your own browser plugin instead.
shipaddicted
Posts: 122
Joined: 15 Jul 2016, 19:57

Re: UIA v2

24 Apr 2024, 20:39

Thank you, @Descolada ! I will test your suggestions tomorrow and see if anything is successful. It did occur to me today to just run the last part of the script twice -- and I guess that will be my fail-safe.

I have tried many, many different methods of scripting Salesforce and haven't had any luck with anything so far, until I started looking into UIA. (I tried chrome.ahk, but can't remember why i gave up.) Yes, I do run into issues with the tree changing in some situations (or the issue mentioned above), but this has given the best, most reliable results so far. I will push this as far as I can I spose as long as it works . . .

I want to give my DEEPEST THANKS for creating this library and hanging to offer all the support. This has been a wealth of information.
shipaddicted
Posts: 122
Joined: 15 Jul 2016, 19:57

Re: UIA v2

27 Apr 2024, 10:34

Can you change the size of an Edit field? I have a Description field that I often have to work with and it is just way too small. Can I enlarge it using UIA? I have managed to get the position, but haven't been able to guess any way that might change the size of the field on the page.

Code: Select all

!m:: 	;change size of description field 
{
	cUIA := ""
	cUIA := UIA_Browser("New Case: Support | Salesforce")
	y := cUIA.GetCurrentDocumentElement() 
	x := 	y.FindElement({Type:"Edit", Name:"*Description"})
	x.ScrollIntoView() 
	z := x.GetPos() 
	 z.w := z.w + 200 
	 z.h := z.h + 200 
	 ; MsgBox z.w " . . . " z.h
	 x.Location.w := z.w
	; Location:{x:2455, y:335, w:263, h: 49}
}
Descolada
Posts: 1431
Joined: 23 Dec 2021, 02:30

Re: UIA v2

28 Apr 2024, 13:45

@shipaddicted I'm not aware of any UIA method capable of resizing an element. Since you are dealing with a website I'd recommend Javascript injection via the address bar, which would probably be fastest and most reliable. Alternatively if the Edit field can be resized by dragging it from the corner, you could use MouseClickDrag to resize it (getting the necessary coordinates with Element.GetPos()).
shipaddicted
Posts: 122
Joined: 15 Jul 2016, 19:57

Re: UIA v2

28 Apr 2024, 15:09

JavaScript works great for most of my work, but every time I try to use it in Salesforce, the webpage completely flips out on me and goes to a really weird error page. Salesforce is a complete mess. Can't stand it.

But MouseClickDrag will probably work for what I need. I'll give it a shot! Thanks!
reddyshyam
Posts: 58
Joined: 24 Jul 2023, 04:34

Re: UIA v2

05 May 2024, 23:32

Hi @Descolada ,

Good day! Is there a way to click into a textarea on the browser using something like below? This doesnot work for some reason. Weird enough if I click anywhere on the page it works unlike Hyperlink or Text type.

Code: Select all

if (cUIA.ElementExist({Type:"Edit", Name:"message", matchmode:"Substring"}))
			cUIA.FindElement({Type:"Edit", Name:"message", matchmode:"Substring"}).SetValue("forums")
At the moment I am using JS injection as shown below but was thinking if above possible, would be easier.

Code: Select all

		js := "
		(
			document.getElementById("message").focus();
			document.getElementById("message").select();
		)"

		;RunJsFromChromeAddressBar(js)
Descolada
Posts: 1431
Joined: 23 Dec 2021, 02:30

Re: UIA v2

06 May 2024, 12:17

@reddyshyam can you post example code that replicates the problem? The following worked on this page just as expected:

Code: Select all

#include UIA.ahk
chromeEl := UIA.ElementFromHandle("UIA v2 - Page 19 - AutoHotkey Community - Google Chrome ahk_exe chrome.exe")
chromeEl.FindElement({Type:"Edit", Name:"Subject", matchmode:"Substring"}).Value := "forums"
anidealworld
Posts: 5
Joined: 17 Feb 2017, 13:07

Re: UIA v2

06 May 2024, 17:48

Anyone know why I'd be getting an error message with this?

Code: Select all

#include .\Lib\UIA.ahk
#include .\Lib\UIA_Browser.ahk
#0::
{ 
	cUIA := UIA_Browser(wTitle="A")
;obviously more code here
}
Warning: This variable appears to never be assigned a value.

Specifically: local wTitle

031: }
037: {
▶ 038: cUIA := UIA_Browser(wTitle="A")
039: }
061: {
I can replace anything with "A" and I always get an error...
shipaddicted
Posts: 122
Joined: 15 Jul 2016, 19:57

Re: UIA v2

06 May 2024, 22:56

FYI -- I have finally determined the reason why it wasn't working was because it wasn't picking up the value at the beginning of the script. So this part was actually working, "statuss" was just blank. So I added a loop -- if statuss was blank, it should attempt up to 5x more to grab the status. If still nuthin, the script notifies me & exits.
Descolada wrote:
24 Apr 2024, 00:38
@shipaddicted unfortunately I don't have access to Salesforce so I can't debug this myself. It sounds like either a wrong element is found inside the switch statement, or it is a timing issue.

My first suggestion is to add some Highlights to see whether everything lights up correctly:

Code: Select all

	;'open' 2nd combobox & select option based on value of 1st combobox 
	y.WaitElement({Type:"ComboBox", Name:"Case Closed Reason",cs:0,mm:2}).Highlight().Invoke() 
	Switch statuss
	{
		case "Stage 1": y.WaitElement({Type:"ListItem", Name:"Paid Stage 1"}).Highlight().Invoke() 
		case "Stage 2": y.WaitElement({Type:"ListItem", Name:"Paid Stage 2"}).Highlight().Invoke() 
		case "Stage 3": y.WaitElement({Type:"ListItem", Name:"Paid Stage 3"}).Highlight().Invoke() 
		case "Stage 4": y.WaitElement({Type:"ListItem", Name:"Paid Stage 4"}).Highlight().Invoke() 
		case "Stage 5": y.WaitElement({Type:"ListItem", Name:"Paid Stage 5"}).Highlight().Invoke() 
		case "Stage 6": y.WaitElement({Type:"ListItem", Name:"Paid Stage 6"}).Highlight().Invoke() 
	}
	MsgBox "done"
If everything is highlighted correctly and the list element is still fake-clicked, then you could try replacing Invoke() with ControlClick() or Click("left") to see whether that makes a difference. Another possibility would be to run that part of the code twice, because you mentioned it works perfectly on the second try (perhaps for some reason the element isn't selected correctly on the first try?).

Also, are you sure that Invoke() is correct there? Comboboxes often have Expand() available, and ListItems may have Select(). They may have different effects than Invoke().

Fyi, I don't recommend UIA for browser automation. It is much more reliable to use Chrome.ahk, Rufaydium, or write your own browser plugin instead.
Descolada
Posts: 1431
Joined: 23 Dec 2021, 02:30

Re: UIA v2

07 May 2024, 00:12

anidealworld wrote:
06 May 2024, 17:48
Anyone know why I'd be getting an error message with this?

Code: Select all

#include .\Lib\UIA.ahk
#include .\Lib\UIA_Browser.ahk
#0::
{ 
	cUIA := UIA_Browser(wTitle="A")
;obviously more code here
}
Warning: This variable appears to never be assigned a value.

Specifically: local wTitle

031: }
037: {
▶ 038: cUIA := UIA_Browser(wTitle="A")
039: }
061: {
I can replace anything with "A" and I always get an error...
In AHK v2 = is an equality comparison operator, not variable assignment. This means you are trying to compare an unset variable to "A" which throws an error. Use either UIA_Browser("A") or UIA_Browser(wTitle:="A") instead.
reddyshyam
Posts: 58
Joined: 24 Jul 2023, 04:34

Re: UIA v2

07 May 2024, 00:55

Descolada wrote:
06 May 2024, 12:17
@reddyshyam can you post example code that replicates the problem? The following worked on this page just as expected:

Code: Select all

#include UIA.ahk
chromeEl := UIA.ElementFromHandle("UIA v2 - Page 19 - AutoHotkey Community - Google Chrome ahk_exe chrome.exe")
chromeEl.FindElement({Type:"Edit", Name:"Subject", matchmode:"Substring"}).Value := "forums"
Hi @Descolada ,

Thanks for getting back. Sorry, I wasn't clear. I do not use UIA lib directly, I always use UIA Browser lib and use code as shown below.

Code: Select all

WinActivate "ahk_exe brave.exe"
WinWaitActive "ahk_exe brave.exe"
cUIA := UIA_Browser("ahk_exe brave.exe")
if (cUIA.ElementExist({Type:"Hyperlink", Name:"Post Reply", matchmode:"Substring"}))
			cUIA.FindElement({Type:"Hyperlink", Name:"Post Reply", matchmode:"Substring"})
Now want to use same variable cUIA and check for textarea if possible instead of JS injection as shown in the previous post.

I even tried your UIA library solution but still in vain.

Code: Select all

chromeEl := UIA.ElementFromHandle(WinGetTitle("ahk_exe brave.exe") " ahk_exe brave.exe")
chromeEl.FindElement({Type:"Edit", Name:"message", matchmode:"Substring"}).Value := "forums"
Also my brave browser has multiple tabs, would that be a reason for not finding the message edit box when using UIA.ElementFromHandle?
roeleboele
Posts: 38
Joined: 24 Nov 2022, 02:39

Re: UIA v2

08 May 2024, 04:36

Hello

I've got a question about the uia
I am loving it but i would like to use an variable to activate an button
for example

Code: Select all

titelEdge := WinGetTitle("A")
msedgeEL:= UIA.ElementFromHandle(titelEdge " ahk_exe msedge.exe")
NaamKlikken := arr[A_index][1]
msedgeEL.FindElement({Name: NaamKlikken , LocalizedType:"Link"}).Click()
I want to click on multiple buttons (the names are in an array) in Edge so i can loop with other buttons and i don't have to hard-code it
I could hardcode the variable "Naamklikken" but that would mean that it is not compatible with other websites where i want to press an button when the array has changed
Anyone who knows how i can fix this

Regards
Descolada
Posts: 1431
Joined: 23 Dec 2021, 02:30

Re: UIA v2

08 May 2024, 10:15

@reddyshyam please post concrete example code and the website address which I could test (and which fails) in Brave browser, because as I've shown what you are doing should work, and if it isn't then probably something unknown is going on.

@roeleboele that is exactly how you can use variables in the condition.
Either

Code: Select all

arr := ["Button Name"]
NaamKlikken := arr[1]
msedgeEL.FindElement({Name: NaamKlikken , LocalizedType:"Link"})
or

Code: Select all

arr := ["Button Name"]
msedgeEL.FindElement({Name: arr[1], LocalizedType:"Link"})
If all the buttons are on the same page and the page doesn't change/refresh in the meanwhile, you could also fetch all the elements in one go with FindElements using an OR condition, eg

Code: Select all

buttons := msedgeEL.FindElements({Name: ["Button1", "Button2"], LocalizedType:"Link"})
button[1].Click()
Also, LocalizedType might change if your browser/computer language changes from English to something else, so it's safer to use Type instead.
reddyshyam
Posts: 58
Joined: 24 Jul 2023, 04:34

Re: UIA v2

08 May 2024, 11:15

Descolada wrote:
08 May 2024, 10:15
@reddyshyam please post concrete example code and the website address which I could test (and which fails) in Brave browser, because as I've shown what you are doing should work, and if it isn't then probably something unknown is going on.
Hi @Descolada,

I am afraid I cannot mention the website as its my work related, sorry but let me use the code to check for edit box on our forum and get back if I am able to get it working. That should be good enough to me. Thanks again!
#include UIA.ahk
chromeEl := UIA.ElementFromHandle("UIA v2 - Page 19 - AutoHotkey Community - Google Chrome ahk_exe chrome.exe")
chromeEl.FindElement({Type:"Edit", Name:"Subject", matchmode:"Substring"}).Value := "forums"
Your above code actually gets

Code: Select all

<input type="text" name="subject" id="subject" >
but I want code to set value or click on to textarea component. On this forum it is

Code: Select all

<textarea style="height: 9em;" name="message" rows="7" cols="76" tabindex="3" class="inputbox"></textarea>
We need anything different to handle textarea?
songdg
Posts: 699
Joined: 04 Oct 2017, 20:04

Re: UIA v2

13 May 2024, 22:10

Hey pal, there is a built-in command to take full-screen screenshots(Capture full size screenshot) in Google Chrome, would you wrap this into a function?
Descolada
Posts: 1431
Joined: 23 Dec 2021, 02:30

Re: UIA v2

13 May 2024, 22:15

@songdg no there isn't such a method, and I won't add it either because it is outside the scope of this library. There are other libraries capable of doing that :)
redrum
Posts: 118
Joined: 25 Jan 2015, 22:57

Re: UIA v2

18 May 2024, 07:16

When the UIA Tree looks like this, I guess it means there are no controls that can be accessed? Specifically, I'd like to access elements in the box that UIA viewer has highlighted in blue.
Screenshot 2024-05-18 081200.png
Screenshot 2024-05-18 081200.png (53.19 KiB) Viewed 3219 times
Screenshot 2024-05-18 081256.png
Screenshot 2024-05-18 081256.png (88.75 KiB) Viewed 3219 times
Descolada
Posts: 1431
Joined: 23 Dec 2021, 02:30

Re: UIA v2

18 May 2024, 08:34

@redrum that is correct, you can't automate it with UIA. Try FindText or other image-based approaches, or just hard-coded coordinates.
shipaddicted
Posts: 122
Joined: 15 Jul 2016, 19:57

Re: UIA v2

20 May 2024, 15:39

How would I select the first element with a non-blank Value?

For example, in the pic below, I searched for a particular email address. When the results didn't show what I needed, I clicked "Search Again," which brought me back to my original search page, but the original email address was already filled in. The only problem is, the page doesn't automatically select the text in the field for you and there's no easy way to get there without a whole lot of tabbing or using the mouse. I want my script to evaluate all "Edit" elements on the page and select all the text on the first one whose Value is not blank. (Normally every single one has a blank Value when you begin a new search.)

I have tried numerous variations of these statements, but I am flying blind here because I didn't find any examples that match what I need:

Code: Select all

cUIA := UIA_Browser("xxx - Google Chrome")
y := cUIA.GetCurrentDocumentElement() 

;none of these work: 
y.FindElement({Type:"Edit", Value: not""}).Select() 
y.FindElement({Type:"Edit", Value: not:""}).Select() 
y.FindElement({Type:"Edit", not:Value: ""}).Select() 

;not sure I understand how to use conditions at all:  
MsgBox cUIA.CreateNotCondition({Type:"Edit", Value:""}).Value
image.png
image.png (30.06 KiB) Viewed 3023 times
I did get this to work, but I was wondering if there might be a more elegant, concise way to get the same results:

Code: Select all

cUIA := UIA_Browser(" - Google Chrome")
	y := cUIA.GetCurrentDocumentElement() 
	x := y.FindElements({Type:"Edit"})
	Loop x.Length
	{
		If (x[A_Index].Value != "") && (x[A_Index].Value != "Search By: All")
		{
			x[A_Index].Select() 
			Send("^a")
		}
	}
}
redrum
Posts: 118
Joined: 25 Jan 2015, 22:57

Re: UIA v2

21 May 2024, 17:51

What I'm trying to do is copy the link address for an image in a discord browser window. There are other ways of doing this (relative mouse move or FindText, but since UIA Viewer can see "Copy link address", I wanted to try using UIA. I left click on the desired image, then run this hotkey, which grabs the hwnd of the selected image:
image.png
image.png (55.48 KiB) Viewed 2948 times

Code: Select all

+F8::
{
        discordImage := UIA.GetFocusedElement()
        discordImage.Click("R")
        Sleep 1000
        discordImage.FindElement({Name:"Copy link address"}).Highlight()
}
The right click performed by the script brings up the right click menu:
image.png
image.png (9.35 KiB) Viewed 2948 times
Element name "Copy link address" is not found:
Error: An element matching the condition was not found
Here's what UIA viewer shows:
image.png
image.png (65.71 KiB) Viewed 2948 times
I am unable to use the macro creator to "add element" - perhaps because the right click menu disappears when left clicking anywhere or changing focus to UIA viewer.

I'm thinking perhaps the problem with my script is the right click menu has a different hwnd than the selected image, whose hwnd I get using UIA.GetFocusedElement()

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: No registered users and 33 guests