Solved

How to select a piece of text in TWebbrowser?

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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