UIA v2

Post your working scripts, libraries and tools.
Descolada
Posts: 1155
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: 106
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: 106
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: 1155
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: 106
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: 43
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: 1155
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: 106
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: 1155
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: 43
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: 33
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: 1155
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: 43
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?

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: No registered users and 37 guests