searching for text within a twebbrowser

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.
LVL 1
hibbidijiAsked:
Who is Participating?
 
Eddie ShipmanConnect With a Mentor All-around developerCommented:
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
 
shaneholmesCommented:
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
 
shaneholmesCommented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
hibbidijiAuthor Commented:
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
 
geobulCommented:
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
 
geobulCommented:
I meant OnDocumentComplete event.
0
 
Eddie ShipmanAll-around developerCommented:
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
 
hibbidijiAuthor Commented:
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
 
Eddie ShipmanAll-around developerCommented:
"pay attention to the text around the spans"
I don't quite understand...

What do you need to do, exactly?
0
 
geobulCommented:
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
 
hibbidijiAuthor Commented:
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
 
hibbidijiAuthor Commented:
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
 
hibbidijiAuthor Commented:
to further clarify - I'm getting undeclared identifier errors
0
 
geobulCommented:
>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
 
Eddie ShipmanAll-around developerCommented:
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
 
hibbidijiAuthor Commented:
working great. I apologize for being a dummy
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.