Here's a sample Delphi project to get the list of all opened Web Browser instances. Use the code to learn how to extract the URL, document HTML, document text and all the tags with their attributes.
Enumerating WebBrowser Windows
To get the list of all active (opened) Web Browser windows, we need to instantiate the TShellWindows class found in the "shDocvw.pas" unit. An item in the list is an IDispatch value. If the item is IWebBrowser2 - the window is a Web Browser window.In Delphi code "words":
procedure TForm1.GetBrowsersButtonClick(Sender: TObject) ;The code above fills a TListBox named "BrowserList" with the URLs of the found Web Browser instances.
var
ShellWindows: TShellWindows;
ShellWindowDisp: IDispatch;
WebBrowser: IWebbrowser2;
Count: integer;
begin
BrowserList.Clear;
ShellWindows := TShellWindows.Create(nil) ;
try
for Count := 0 to ShellWindows.Count - 1 do
begin
ShellWindowDisp := ShellWindows.Item(Count) ;
if ShellWindowDisp = nil then Continue;
ShellWindowDisp.QueryInterface(iWebBrowser2, WebBrowser) ;
if WebBrowser.LocationURL = '' then Continue;
if Assigned(WebBrowser.Document) then
BrowserList.Items.Add(WebBrowser.LocationURL) ;
end;
finally
ShellWindows.Free;
end;
end;
WebBrowser Info
For a selected web browser document, we can simply extract the HTML, document's text and all the tags:...Download Web Browser Enumerator
Document := WebBrowser.Document;
...
DocTextMemo.Lines.Text := Document.Body.innerText;
DocHtmlMemo.Lines.Text := Document.Body.innerHTML;
for ElementCount := 0 to Document.All.length - 1 do
begin
DocElement := Document.All.Item(ElementCount) ;
ElementNode := NodeTree.Items.Add(nil, DocElement.TagName) ;
if DocElement.innerHTML <> '' then NodeTree.Items.AddChild(NodeTree.Items.AddChild(ElementNode, 'HTML'), DocElement.innerHTML) ;
if DocElement.innerText <> '' then NodeTree.Items.AddChild(NodeTree.Items.AddChild(ElementNode, 'Text'), DocElement.innerText) ;
end;


