Descolada / UIA-v2

UIAutomation library for AHK v2, based on thqby's UIA library
MIT License
159 stars 22 forks source link

GetTab and CloseTab: Unable to get tab elements (Vivaldi) #46

Open emp-00 opened 6 days ago

emp-00 commented 6 days ago

Using Vivaldi browser, the below script fails with the message "Please file a bug report"

Symptom: NewTab() works fine but GetTab() fails. I'm using the latest UIA_Browser library and I have initialized cUIA := UIA_Browser() Additional info: allnames := cUIA.GetAllTabNames() works fine. As a workaround I tried CloseTab(allnames[1]) on my target Tab but here CloseTab failed as well, same error message as below. Looks like this issue is connected to the same root cause? Thanks for looking into this.

Error: Unable to get tab elements Specifically: Please file a bug report 002: cUIA.NewTab() 003: Sleep(500) ▶ 004: hello := cUIA.GetTab() 005: Sleep(500) 006: cUIA.CloseTab(hello) The current thread will exit.

What I actually want to achieve is "simply" this: NewTab --> wait 500 ms --> (reliably) close the newly opened tab again, while making sure not to close any other tab.

Descolada commented 6 days ago

I am unable to reproduce this in Windows 10 running latest Vivaldi. Could you post the output of

A_Clipboard := cUIA.TabBarElement.DumpAll()
emp-00 commented 6 days ago

With further testing I confirm - the error is not really reproducible (any more!) ... Very strange. For reference, I still post the TabBarElement.DumpAll() below with two tabs open, Maybe you can still detect something odd there? I have to admit that normally I have around 30 tabs open with many being "Tab Stacks" with a bunch of sub-tabs, maybe this is also part of the original issue?

Furthermore, I have simplified my script to below code, it reloads the current active tab, opens a NewTab and closes the new tab again. Is it normal, that such long Sleep is neccessary? Any Sleep below ~700 ms wrongly closes the current active tab and not the NewTab (which should be captured in the variable "hello")... Would it be possible to detect the NewTab to be fully "loaded" so that the variable always contains the correct reference and CloseTab works "as fast as possible" as well as in different CPU load conditions?

For the actual issue posted above, for now this is indeed "gone".

cUIA := UIA_Browser() 
cUIA.Reload()
hello := cUIA.NewTab()
Sleep(1000) ; This sleep is ESSENTIAL !!!!
cUIA.CloseTab(hello)

TabBarElement.DumpAll():

Type: 50018 (Tab) Name: "Tabs" LocalizedType: "tab"
1: Type: 50000 (Button) Name: "New Tab" LocalizedType: "button"
1,1: Type: 50006 (Image) LocalizedType: "image"
2: Type: 50019 (TabItem) Name: "International Business, World News & Global Stock Market Analysis" LocalizedType: "tab item" AutomationId: "tab-1907715971"
2,1: Type: 50006 (Image) LocalizedType: "image"
2,2: Type: 50020 (Text) Name: "International Business, World News & Global Stock Market Analysis" LocalizedType: "text"
3: Type: 50019 (TabItem) Name: "Test.com is for sale" LocalizedType: "tab item" AutomationId: "tab-1907715981"
3,1: Type: 50006 (Image) LocalizedType: "image"
3,2: Type: 50020 (Text) Name: "Test.com is for sale" LocalizedType: "text"
4: Type: 50019 (TabItem) Name: "Start Page" LocalizedType: "tab item" AutomationId: "tab-1907715989"
4,1: Type: 50006 (Image) LocalizedType: "image"
4,2: Type: 50020 (Text) Name: "Start Page" LocalizedType: "text"
Descolada commented 6 days ago

So I did find a minor bug in UIA_Vivaldi.GetTab and pushed a fix, but I'm not sure it was the cause of your problems.

With Vivaldi the problem is that we can't detect which tab is active with UIA. UIA_Browser uses a work-around for that: it extracts the tab name from the window title and then searches for a tab with that name. However, that method will fail if you create a tab and then very quickly try to access it, because the window title will not have had time to change yet. You can work around that by specifying the newly created tab name, which in Vivaldi is "Start Page". Also note that cUIA.NewTab() does not return an element nor a string, which means your hello variable will always be empty.

Try this:

cUIA.NewTab()
cUIA.CloseTab("Start Page")
emp-00 commented 5 days ago

Understood and thanks so much for your debugging efforts!

The NewTab / CloseTab("Start Page") works - but only with a Sleep 700+ ms in between.

Don't know if it helps, I guess you probably cannot circumvent this - but FYI this error is thrown without (or too short) sleep. Sounds like a "prototype-function" is used, is this all as expected?

Error: No search phrase matches found

---- C:\Tools\AutoHotkey_v2\Libs\UIA-v2-main\Lib\UIA_Browser.ahk 838: Try 838: { ▶ 839: selectedTab := IsObject(tabName) ? tabName : this.GetTab(tabName, matchMode, caseSense) 840: selectedTab.Click() 841: }

The current thread will exit.

Call stack: C:\Tools\AutoHotkey_v2\Libs\UIA-v2-main\Lib\UIA_Browser.ahk (178) : [UIA_Vivaldi.Prototype.GetTab] Throw Error("No search phrase matches found", -1) C:\Tools\AutoHotkey_v2\Libs\UIA-v2-main\Lib\UIA_Browser.ahk (839) : [UIA_Browser.Prototype.SelectTab] selectedTab := IsObject(tabName) ? tabName : this.GetTab(tabName, matchMode, caseSense) C:\Tools\AutoHotkey_v2\Libs\UIA-v2-main\Lib\UIA_Browser.ahk (193) : [UIA_Vivaldi.Prototype.CloseTab] this.SelectTab(tabElementOrName) C:\XXXX\XXX.ahk (85) : [] cUIA.CloseTab("Start Page")