Skip to content

12. UIA_Browser

Descolada edited this page Feb 6, 2023 · 1 revision

Introduction

UIA_Browser.ahk is an add-on to UIA.ahk to more easily automate browsers: it contains methods for page navigation, page load detection, Javascript injection etc. Currently UIA_Browser supports Chrome and Edge, and to a lesser extent Firefox.

Getting started

Initiate a new instance of UIA_Browser with cUIA := UIA_Browser(wTitle=""). This will "hook" onto the browser and will pre-fetch some of the commonly used elements inside a browser window (Document element, URL bar etc).

Instances for specific browsers may be initiated with UIA_Chrome, UIA_Edge, UIA_Mozilla (arguments are the same as for UIA_Browser), but this is usually not necessary because the browser type is detected automatically by UIA_Browser. UIA_Mozilla can be created with an additional javascriptExecutionMethod argument UIA_Mozilla(wTitle:="", javascriptExecutionMethod:="Console"): by default Javascript injection will be done by opening the console and pasting the code there, but this can also be set to "Bookmark" to allow execution through the URL bar. For this you need to create a new bookmark with URL "javascript:%s" and keyword "javascript".

Through the UIA_Browser object you can access to the browser element, UIA object, and UIA_Browser methods/properties. This means that searching for page elements can be done with cUIA.FindElement, and changing UIA properties can be done with either UIA.Property := value or cUIA.Property := value.

UIA_Browser properties

BrowserId

ahk_id of the browser window

BrowserType

"Chrome", "Edge", "Mozilla" or "Unknown"

BrowserElement

The browser window element, which can also be accessed by just calling an element method from UIA_Browser (cUIA.FindElement would call FindElement method on the BrowserElement, is equal to cUIA.BrowserElement.FindElement)

MainPaneElement

Element for the upper part of the browser containing the URL bar, tabs, extensions etc

URLEditElement

Element for the address bar

UIA_Browser methods

GetCurrentMainPaneElement

GetCurrentMainPaneElement() Refreshes UIA_Browser.MainPaneElement and also returns it

GetCurrentDocumentElement

GetCurrentDocumentElement() Returns the current document/content element of the browser.

GetAllText

GetAllText() Gets all text from the browser element (Name properties for all child elements)

GetAllLinks

GetAllLinks() Gets all link elements from the browser (returns an array of HyperLink elements)

WaitTitleChange

WaitTitleChange(targetTitle:="", timeOut:=-1) Waits the browser title to change to targetTitle (by default just waits for the title to change), timeOut is in milliseconds (default is indefinite waiting)

WaitPageLoad

WaitPageLoad(targetTitle:="", timeOut:=-1, sleepAfter:=500, titleMatchMode:=3, titleCaseSensitive:=True) Waits the browser page to load to targetTitle, default timeOut is indefinite waiting, sleepAfter additionally sleeps for 500ms after the page has loaded. Optionally a specific targetTitle can be specified to improve reliability.

Back

Back() Presses the Back button

Forward

Forward() Presses the Forward button

Reload

Reload() Presses the Reload button

Home

Home() Presses the Home button if one exists.

GetCurrentURL

GetCurrentURL(fromAddressBar:=False) Gets the current URL. fromAddressBar=True gets it straight from the URL bar element, which is not a very good method, because the text might be changed by the user and doesn't start with "http(s)://". Default of fromAddressBar=False will cause the real URL to be fetched, but the browser must be visible for it to work (if is not visible, the window will be automatically activated).

SetURL

SetURL(newUrl, navigateToNewUrl:=False) Sets the URL bar to newUrl, optionally also navigates to it if navigateToNewUrl=True

Navigate

Navigate(url, targetTitle:="", waitLoadTimeOut:=-1, sleepAfter:=500) Navigates to URL and waits page to load

NewTab

NewTab() Presses the New tab button.

GetTab

GetTab(searchPhrase:="", matchMode:=3, caseSensitive:=True) Returns a tab element with text of searchPhrase, or if empty then the currently selected tab. If no tab is found with the specified searchPhrase, TargetError is thrown. matchMode follows SetTitleMatchMode scheme: 1=tab name must must start with tabName; 2=can contain anywhere; 3=exact match; RegEx

TabExist

TabExist(searchPhrase:="", matchMode:=3, caseSensitive:=True) Checks whether a tab element with text of searchPhrase exists: if a matching tab is found then the element is returned, otherwise 0 is returned. matchMode follows SetTitleMatchMode scheme: 1=tab name must must start with tabName; 2=can contain anywhere; 3=exact match; RegEx

GetTabs

GetTabs(searchPhrase:="", matchMode:=3, caseSensitive:=True) Returns all tab elements with text of searchPhrase, or if empty then all tabs. matchMode follows SetTitleMatchMode scheme: 1=tab name must must start with tabName; 2=can contain anywhere; 3=exact match; RegEx

GetAllTabNames

GetAllTabNames() Gets all the titles of tabs (returns an array)

SelectTab

SelectTab(tabName, matchMode:=3, caseSensitive:=True) Selects a tab with the text of tabName. matchMode follows SetTitleMatchMode scheme: 1=tab name must must start with tabName; 2=can contain anywhere; 3=exact match; RegEx

CloseTab

CloseTab(tabElementOrName:="", matchMode:=3, caseSensitive:=True) Close tab by either providing the tab element or the name of the tab. If tabElementOrName is left empty, the current tab will be closed. If no tab is found by the specified name, TargetError is thrown.

IsBrowserVisible

IsBrowserVisible() Returns True if any of the 4 corners of the browser are visible.

Send

Send(text) Uses ControlSend to send text to the browser.

GetAlertText

GetAlertText() Gets the text from an alert box

CloseAlert

CloseAlert() Closes an alert box

JSExecute

JSExecute(js) Executes Javascript code using the address bar

NOTE: In Firefox this is done by default through the console, which is a slow and inefficient method. A better way is to create a new bookmark with URL "javascript:%s" and keyword "javascript". This allows executing javascript through the address bar with "javascript alert("hello")". To make JSExecute use this method, either create UIA_Mozilla with JavascriptExecutionMethod set to "Bookmark", or set cUIA.JavascriptExecutionMethod := "Bookmark". Default value is "Console".

JSReturnThroughClipboard

JSReturnThroughClipboard(js) Executes Javascript code using the address bar and returns the return value of the code using the clipboard (resetting it back afterwards)

JSReturnThroughTitle

JSReturnThroughTitle(js, timeOut:=500) Executes Javascript code using the address bar and returns the return value of the code using the browsers title (resetting it back afterwards). This might be unreliable, so the clipboard method is recommended instead.

JSSetTitle

JSSetTitle(newTitle) Uses Javascript through the address bar to change the title of the browser

JSGetElementPos

JSGetElementPos(selector, useRenderWidgetPos:=False) Uses Javascript's querySelector to get a Javascript element and then its position. useRenderWidgetPos=True uses position of the Chrome_RenderWidgetHostHWND1 control to locate the position element relative to the window, otherwise it uses UIA_Browsers CurrentDocumentElement position.

JSClickElement

JSClickElement(selector) Uses Javascript's querySelector to get and click a Javascript element

ControlClickJSElement

ControlClickJSElement(selector, WhichButton:="", ClickCount:="", Options:="", useRenderWidgetPos:=False) Uses Javascript's querySelector to get a Javascript element and then ControlClicks that position. useRenderWidgetPos=True uses position of the Chrome_RenderWidgetHostHWND1 control to locate the position element relative to the window, otherwise it uses UIA_Browsers CurrentDocumentElement position.

ClickJSElement

ClickJSElement(selector, WhichButton:="", ClickCount:=1, DownOrUp:="", Relative:="", useRenderWidgetPos:=False) Uses Javascript's querySelector to get a Javascript element and then Clicks that position. useRenderWidgetPos=True uses position of the Chrome_RenderWidgetHostHWND1 control to locate the position element relative to the window, otherwise it uses UIA_Browsers CurrentDocumentElement position.