[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

searching for text within a twebbrowser

Posted on 2005-04-19
16
Medium Priority
?
262 Views
Last Modified: 2010-04-05
I would like to take one action if a particular string appears on the page that is loaded by my twebbrowser and another if it doesnt.    How can I do this?   I'm not sure where I can get the html contents of the control.
0
Comment
Question by:hibbidiji
  • 6
  • 4
  • 4
  • +1
16 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 13819626
procedure TForm1.Button2Click(Sender: TObject);
var
  DOM : variant;
  SL: TStringList;
begin
  SL:= TSTringList.Create;
  DOM := WebBrowser1.Document;
  if Webbrowser1.LocationURL <> '' then
    begin
      SL.Text := DOM.Body.OuterHTML;
    end
    else
    begin
          ShowMessage('No page available!');
    end;
end;

Now you can iterate through the StringList and find what your looking for


ShaneHolmes
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 13819663
procedure TForm1.Button1Click(Sender: TObject);
var
 DOM : variant;
 SL: TSTringList;
 I: Integer;
begin
 WebBrowser1.Navigate('Www.google.com');
 DOM := WebBrowser1.Document;
 DOM.Body.OuterHTML;
 SL:= TSTringList.Create;
 SL.Text:= DOM.Body.OuterHTML;
 for I:= 0 to SL.Count - 1 do
 begin
  // if  Pos('SomeText', SL[I]) > 0 then
  // begin
  //  //do somthing
  // end;
 end;
end;

ShaneHolmes
0
 
LVL 1

Author Comment

by:hibbidiji
ID: 13821411
I have increased points to 400 because I need to further refine my question.

I need to parse some html within the strinlist that we're looping through.

It will look like this:
There are  <span class="ytext">4235</span> records.

I need to do a comparison of the number inside the class span.    If it is more than 40 I need to execute my "something"   if not, I need to do nothing.    I suck eggs at regexp so I could use some assistance.

Thanks, and if this question cant be answered by shaneholmes, shane will get 100% of the original points with the rest to the person who answers the rest.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:geobul
ID: 13822242
Hi,

Use OnNavigateComplete event with the following code:

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  doc: variant;
  sl: TStringList;
  s: string;
  i, k: integer;
  span: integer;
begin
  try
    sl := TStringList.Create;
    try
      doc := WebBrowser1.Document;
      sl.Text := doc.Body.OuterHTML;
      for i := 0 to sl.Count - 1 do begin
        k := Pos('<span', LowerCase(sl[i]));
        if k > 0 then begin
          s := Copy(sl[i], k + 5, Length(sl[i]));
          k := Pos('>', s);
          if k > 0 then begin
            s := Copy(s, k + 1, Length(sl[i]));
            k := Pos('<', s);
            if k > 0 then begin
              try
                span := StrToInt(Copy(s, 1, k - 1));
                if span > 40 then begin
                  // add your code here
                  // ....
                  break;
                end;
              except
              end;
            end;
          end;
        end;
      end;
    finally
      sl.Free;
    end;
  except
  end;
end;

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 13822246
I meant OnDocumentComplete event.
0
 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 13825066
Here's how to do it using the DOM:

uses  ...,mshtml; // if using D6 or greater, else mshtml_tlb after importing the TypeLib

procedure TForm1.Button1Click(Sender: TObject);
begin
 { HTML source for span.html:
    <html>
      <head>
        <title>Untitled</title>
      </head>
      <body>
        <span class="ytext1">4235</span>
        <span class="ytext2">5</span>
        <span class="ytext3">35</span>
      </body>
    </html>
 }
  WB.Navigate('http://localhost/span.html');
end;

procedure TForm1.WBDocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  doc: IHTMLDocument2;
  elements: IHTMLElementCollection;
  element: IHTMLElement;
  i:        Integer;
begin
  // Get the WebBrowser's Document into an IHTMLDocument2 object
  doc := WB.Document as IHTMLDocument2;
  // Get all the Span elements
  elements := doc.all.tags('SPAN') as IHTMLElementCollection;
  // iterate through them
  for i := 0 to elements.length-1 do
  begin
    select each element into an IHTMLElement object
    element := elements.item(i, '') as IHTMLElement;
    // Check to see if classname is OK, not sure if you want to do this.
    if Pos('ytext', element.className) > 0 then
    begin
      // May need to do more checking before this conversion if these spans contain other text.
      if StrToInt(Trimelement.innerText)) > 40 then
      begin
        ShowMessage('Found One: ' + element.innerText);
      end;
    end;
  end;
end;
0
 
LVL 1

Author Comment

by:hibbidiji
ID: 13827975
Yes, there are many spans on the page that will use the same style AND have numeric values.   I like the idea of using dom, but I am at a loss as to how to use your example AND pay attention to the text around the spans
There are  <span class="ytext">4235</span> records.

Ideas?
0
 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 13834194
"pay attention to the text around the spans"
I don't quite understand...

What do you need to do, exactly?
0
 
LVL 17

Expert Comment

by:geobul
ID: 13834311
That's the question! What do you need? If there are many SPAN tags containing different numbers then what will you be searching for?
0
 
LVL 1

Author Comment

by:hibbidiji
ID: 13837470
There may be 200 span tags containing a number

I am only interested in the one that contains the number of records.   It will be:
There are  <span class="ytext">(int here)</span> records.
every single time
0
 
LVL 26

Accepted Solution

by:
Eddie Shipman earned 1600 total points
ID: 13838760
Oh, you didn't SAY that!!!
Will it ALWAYS say "There are xxxxxx records."?
And will it ALWAYS be "<span class="ytext">"?

If so, try this:

procedure TForm1.WBDocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  doc: IHTMLDocument2;
  elements: IHTMLElementCollection;
  element: IHTMLElement;
  i:        Integer;
  x, y:     Integer;
  s:        String;
begin
  // Get the WebBrowser's Document into an IHTMLDocument2 object
  doc := WB.Document as IHTMLDocument2;
  // Get all the Span elements
  elements := doc.all.tags('SPAN') as IHTMLElementCollection;
  // iterate through them
  for i := 0 to elements.length-1 do
  begin
    // select each element into an IHTMLElement object
    element := elements.item(i, '') as IHTMLElement;
    // Check to see if classname is OK, not sure if you want to do this.
    if element.className = 'ytext' then
    begin
      // May need to do more checking before this conversion if these spans contain other text.
      s := element.parentElement.innerText;
      x := Pos('are', S) + 4;
      y := Pos(' records', S);
      s := Copy(s, x, y-x);
      ShowMessage('There are ' + s + ' records.');
    end;
  end;
end;


0
 
LVL 1

Author Comment

by:hibbidiji
ID: 13839276
Eddie,
  doc: IHTMLDocument2;
  elements: IHTMLElementCollection;
  element: IHTMLElement;

These are giving me errors.  do the names need to be changed to match something in my app?   Please forgive my ignorance on this.
0
 
LVL 1

Author Comment

by:hibbidiji
ID: 13839438
to further clarify - I'm getting undeclared identifier errors
0
 
LVL 17

Expert Comment

by:geobul
ID: 13840782
>uses  ...,mshtml; // if using D6 or greater, else mshtml_tlb after importing the TypeLib

Import 'Microsoft HTML Object Library' from 'Project-Import Type Library' menu.
0
 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 13842502
I thought you had tried the last code I posted which would have told you to include
mshtml or mshtml_tlb in your uses... ;-)
I was only modifying the original code I posted.
0
 
LVL 1

Author Comment

by:hibbidiji
ID: 13846409
working great. I apologize for being a dummy
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

830 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