Solved

How to select a piece of text in TWebbrowser?

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

773 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