Solved

How to select a piece of text in TWebbrowser?

Posted on 2004-10-17
5
178 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
  • 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now