DelphiFAQ Home Search:

Accessing the text in TWebBrowser

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

Question:

I want to copy the text of a web page that I loaded with TWebBrowser. I tried

Document := WebBrowser1.Document as IHTMLDocument2;
s := Document.body.innerText;


but this does not include fields that are populated at run-time by javascript.

Answer:

You need to set the focus on the TWebBrowser control (another challenge), then select-all text by simulating a Control+A key stroke, and afterwards copy the selected text into the clipboard.

procedure mySleep(ms : integer);
var
  st : Longint;
begin
  st := GetTickCount;
  while GetTickCount-st<=ms do
    Application.ProcessMessages;
end;

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  Document : IHTMLDocument2;
  s : string;
  f : TextFile;
  sFn : string;
  i : integer;
begin
  // save the browser content to a text file
  sFn := sCaptureFolder + currentProduct + '_' + currentArea + '.screen.txt';

  mySleep(5000);

{
  Anyone that has worked with TWebBrowser knows that the TWebBrowser.SetFocus
  procedure doesn't set focus on the "web page", but rather on the control
  itself, which doesn't do us a whole lot of good. Here is how you can set
  focus on the "web page" (i.e., document)
}
  SetCursorPos(Form1.Left+30, Form1.Top+100);
  mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
  mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

  with WebBrowser1 do
    if Document <> nil then
      with Application as IOleobject do
        DoVerb(OLEIVERB_UIACTIVATE, nil, WebBrowser1, 0, Handle, GetClientRect);

  // send Control-A
  keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), 0, 0);
  keybd_event(ord('A'), MapvirtualKey(ord('A'), 0), 0, 0);
  keybd_event(ord('A'), MapvirtualKey(ord('A'), 0), KEYEVENTF_KEYUP, 0);
  keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), KEYEVENTF_KEYUP, 0);

  mySleep(500);

  keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), 0, 0);
  keybd_event(VK_INSERT, MapvirtualKey(VK_INSERT, 0), KEYEVENTF_EXTENDEDKEY, 0);
  keybd_event(VK_INSERT, MapvirtualKey(VK_INSERT, 0), KEYEVENTF_EXTENDEDKEY + KEYEVENTF_KEYUP, 0);
  keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), KEYEVENTF_KEYUP, 0);

  mySleep(2000);

  // put it into a Delphi control
  SetCursorPos(Form1.Left+Form1.Width-50, Form1.Top+100);

  mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
  mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

  Memo1.SetFocus;
  SetForegroundWindow(Memo1.Handle);

  mySleep(500);

//  keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), 0, 0);
//  keybd_event(VK_INSERT, MapvirtualKey(VK_INSERT, 0), KEYEVENTF_EXTENDEDKEY, 0);
//  keybd_event(VK_INSERT, MapvirtualKey(VK_INSERT, 0), KEYEVENTF_EXTENDEDKEY + KEYEVENTF_KEYUP, 0);
//  keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), KEYEVENTF_KEYUP, 0);
  keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), 0, 0);
  keybd_event(ord('V'), MapvirtualKey(ord('V'), 0), 0, 0);
  keybd_event(ord('V'), MapvirtualKey(ord('V'), 0), KEYEVENTF_KEYUP, 0);
  keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), KEYEVENTF_KEYUP, 0);

  mySleep(1000);

  Memo1.Lines.SaveToFile(sFn);
end;


// the following is necessary to make
// the clipboard work
initialization
  OleInitialize(nil);

finalization
  OleUninitialize


Content-type: text/html

Comments:

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: