I adapted the code below to capture url of active tab of main browsers. Seems work fine, but in:
- Mozilla Firefox
- Microsoft Edge
- Internet Explorer
more than on control is treated as type > UIA_EditControlTypeId
and your value is empty.
For example, on Mozilla Firefox the active tab and url address bar is of type UIA_EditControlTypeId
.
Already on Internet Explorer seems have two url address bar, being one without value.
Some idea about how fix this?
uses
UIAutomationClient_TLB, Activex;
...
type
TTargetWndInfo = record
hWindow: hwnd;
ClassName: string;
end;
PTargetWndInfo = ^TTargetWndInfo;
function EnumVisbleNoOwnedWndProc(hWindow: hwnd; lData: PTargetWndInfo): BOOL; stdcall;
function IsOwnedWindow(hWindow: hwnd): Boolean;
begin
Result := GetWindow(hWindow, GW_OWNER) <> 0;
end;
function GetClassNameStr(hWindow: hwnd): string;
var
Buffer: array[0..MAX_PATH - 1] of Char;
Len: Integer;
begin
FillChar(Buffer, SizeOf(Buffer), #0);
Len := GetClassName(hWindow, Buffer, Length(Buffer));
if Len > 0 then
Result := Buffer
else
Result := EmptyStr;
end;
var
LClassName: string;
begin
Result := True;
if IsOwnedWindow(hWindow) then
Exit;
if not IsWindowVisible(hWindow) then
Exit;
LClassName := UpperCase(GetClassNameStr(hWindow));
if LClassName = UpperCase(lData^.ClassName) then
begin
lData^.hWindow := hWindow;
Result := False;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
UIAuto: TCUIAutomation;
Element, ItemElement: IUIAutomationElement;
Condition: IUIAutomationCondition;
Collection: IUIAutomationElementArray;
ValuePattern: IUIAutomationValuePattern;
Inter: IInterface;
WndInfo: TTargetWndInfo;
TargetWnd: HWND;
Length: Integer;
I: Integer;
RetValue: Integer;
Name, Value: WideString;
begin
WndInfo.ClassName := 'Chrome_WidgetWin_1' {'MozillaWindowClass' 'IEFrame'};
WndInfo.hWindow := 0;
EnumWindows(@EnumVisbleNoOwnedWndProc, LPARAM(@WndInfo));
TargetWnd := WndInfo.hWindow;
if TargetWnd = 0 then
Exit;
UIAuto := TCUIAutomation.Create(nil);
UIAuto.CreateTrueCondition(Condition);
UIAuto.ElementFromHandle(TargetWnd, Element);
Element.FindAll(TreeScope_Subtree, Condition, Collection);
Collection.Get_Length(Length);
for I := 0 to Pred(Length) do
begin
Collection.GetElement(I, ItemElement);
ItemElement.Get_CurrentControlType(RetValue);
if (RetValue = UIA_EditControlTypeId) then
begin
ItemElement.Get_CurrentName(Name);
ItemElement.GetCurrentPattern(UIA_ValuePatternId, Inter);
Inter.QueryInterface(IID_IUIAutomationValuePattern, ValuePattern);
ValuePattern.Get_CurrentValue(Value);
{if Value <> EmptyWideStr then
begin}
Form1.Memo1.Lines.Add((Trim(Value)));
{Break;
end;}
end;
end;
UIAuto.Free;
end;
Edition:
Sincerely i think that all is working fine. This was a choose of UIAutomation decide about some elements be of type UIA_EditControlTypeId
(even that user not seeing as a edit field).