Solved

How to select a piece of text in TWebbrowser?

Posted on 2004-10-17
5
188 Views
Last Modified: 2010-04-05
For example, I've got a dynamic web page. For instance, this is a part of the content of the web page:

...
August 24, 2004
Today's Quote of the Day: 'The first string between the quotation mark'
...

the day after that, the part is:

...
August 25, 2004
Today's Quote of the Day: 'The second string between the quotation mark'
...

What I want to do is getting only the string between the quotation mark. How can I do that?
0
Comment
Question by:cctest
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 

Accepted Solution

by:
Jana618 earned 125 total points
ID: 12336283
Most reliably, looking at the source of the page, if the item you want always appears at a given tab index or field name you can retrieve the text using a TWebBrowser by:

(I think it needs mshtml in the uses clause, maybe other?)
var
  i, j: Integer;
  FormItem: Variant;
begin
  try
    //no form on document
    if WebBrowser.OleObject.Document.all.tags('FORM').Length = 0 then
      Exit;
    //count forms on document
    for I := 0 to WebBrowser.OleObject.Document.forms.Length - 1 do
    begin
      FormItem := WebBrowser.OleObject.Document.forms.Item(I);
      for j := 0 to FormItem.Length - 1 do
      begin
          try

            //BY TAB INDEX
            if j = TabIndex then begin
              OutputRichEdit.Lines.Text := FormItem.Item(j).Value;
              Exit;
            end;
          except
            Exit;
          end;

          //OR BY FIELD NAME
            if WideSameText(FormItem.Item(j).Name, FieldName) then begin
              OutputRichEdit.Lines.Text := FormItem.Item(j).Value;
              Result := True;
              Exit;
            end;
          except
            Exit;
          end;

        end;

Otherwise you could try HTML page scraping:
http://delphi.about.com/library/weekly/aa062502a.htm 

Or, if the text on the page around the quote you want is static, you could retrieve only the displayed text from the page (for my example if you can cast as an IHTMLDocument2) and locate the unkown text you want via the same kind of stripping of the known text (ie Today's Quote of the Day: ') from either side...

(again, I think it needs mshtml in the uses clause, maybe other?)
  var
    txtRnge: IHTMLTxtRange; //TextRange Object
  begin
      try
        txtRnge := ((MyWebBrowser.Document as IHTMLDocument2).body
          as IHTMLBodyElement).createTextRange;
        OutputRichEdit.Lines.Text := txtRnge.Get_text;
      except
          Exit;
      end;

Good luck...
Jana
0
 

Expert Comment

by:Jana618
ID: 13359454
there are three viable methods here...
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question