TWebBrowser控件 相关内容

来源:互联网 发布:winrar软件破解版 编辑:程序博客网 时间:2024/06/06 05:16

1、WebBrowser.OleObject.Document:

var

document: IHTMLDocument2; 
begin 
document := WebBrowser.Document as IHTMLDocument2; 
if Assigned(document) then 


2、WebBrowser. OleObject. Document. All:

var

document: IHTMLDocument2; 
docAll: IHTMLElementCollection; 
firstElement: IHTMLElement; 
begin 
document := WebBrowser.Document as IHTMLDocument2; 
if Assigned(document) then 
begin 
    docAll := document.all; 
    firstElement := docAll.Item(0,''); 

3、WebBrowser.OleObject.Document.Forms: 
var 
htmlDoc: IHTMLDocument2; 
allForms: IHTMLElementCollection; 
firstForm: IHTMLFormElement; 
begin 
htmlDoc := WebBrowser.Document as IHTMLDocument2; 
allForms := htmlDoc.Forms; 
firstForm := allForms.Item(0,'') as IHTMLFormElement; 

3.1、得到一个页面中form的数目: 
function NumberOfForms(document: IHTMLDocument2): integer; 
var 
forms: IHTMLElementCollection; 
begin 
forms := document.Forms as IHTMLElementCollection; 
result := forms.Length; 
end; 
3.2、通过form的数字序号取得form: 
function GetFormByNumber(document: IHTMLDocument2; 
           formNumber: integer): IHTMLFormElement; 
var 
forms: IHTMLElementCollection; 
begin 
forms := document.Forms as IHTMLElementCollection; 
if formNumber < forms.Length then 
     result := forms.Item(formNumber,'') as IHTMLFormElement 
else 
    result := nil; 
end; 
3.3、取得某一form的名字: 
var 
firstForm: IHTMLFormElement; 
document: IHTMLDocument2; 
begin 
document := WebBrowser.Document as IHTMLDocument2; 
firstForm := GetFormByNumber(document,0); 
if Assigned(firstForm) then 
     ShowMessage('Name of first form is ' + firstForm.Name) 
else 
     ShowMessage('This page does not contain any forms'); 
3.4、通过form名字取得form: 
function GetFormByName(document: IHTMLDocument2; 
        const formName: string): IHTMLFormElement; 
var 
   forms: IHTMLElementCollection; 
begin 
   forms := document.Forms as IHTMLElementCollection; 
   result := forms.Item(formName,'') as IHTMLFormElement 
end; 
3.5、List the names of all the fields on a form: 
function GetFormFieldNames(fromForm: IHTMLFormElement): TStringList; 
var 
index: integer; 
field: IHTMLElement; 
input: IHTMLInputElement; 
select: IHTMLSelectElement; 
text: IHTMLTextAreaElement; 
begin 
result := TStringList.Create; 
for index := 0 to fromForm.length do 
begin 
     field := fromForm.Item(index,'') as IHTMLElement; 
if Assigned(field) then 
begin 
     if field.tagName = 'INPUT' then 
     begin 
       // Input field. 
         input := field as IHTMLInputElement; 
         result.Add(input.name); 
      end 
      else if field.tagName = 'SELECT' then 
       begin 
         // Select field. 
          select := field as IHTMLSelectElement; 
          result.Add(select.name); 
       end 
      else if field.tagName = 'TEXTAREA' then 
        begin 
          // TextArea field. 
          text := field as IHTMLTextAreaElement; 
          result.Add(text.name); 
        end; 
      end; 
    end; 
end; 
3.6、Get the value of a named field: 
function GetFieldValue(fromForm: IHTMLFormElement; 
                   const fieldName: string): string; 
var 
field: IHTMLElement; 
inputField: IHTMLInputElement; 
selectField: IHTMLSelectElement; 
textField: IHTMLTextAreaElement; 
begin 
field := fromForm.Item(fieldName,'') as IHTMLElement; 
if not Assigned(field) then 
    result := '' 
else 
   begin 
     if field.tagName = 'INPUT' then 
     begin 
       inputField := field as IHTMLInputElement; 
       result := inputField.value 
     end 
     else if field.tagName = 'SELECT' then 
          begin 
             selectField := field as IHTMLSelectElement; 
             result := selectField.value 
          end 
     else if field.tagName = 'TEXTAREA' then 
          begin 
            textField := field as IHTMLTextAreaElement; 
            result := textField.value; 
           end; 
     end 
end; 
3.7、Set the value of a named field: 
procedure SetFieldValue(theForm: IHTMLFormElement; 
        const fieldName: string; const newValue: string); 
var 
field: IHTMLElement; 
inputField: IHTMLInputElement; 
selectField: IHTMLSelectElement; 
   textField: IHTMLTextAreaElement; 
begin 
   field := theForm.Item(fieldName,'') as IHTMLElement; 
   if Assigned(field) then 
    begin 
       if field.tagName = 'INPUT' then 
       begin 
         inputField := field as IHTMLInputElement; 
          inputField.value := newValue; 
       end 
    else if field.tagName = 'SELECT' then 
        begin 
          selectField := field as IHTMLSelectElement; 
          selectField.value := newValue; 
        end 
     else if field.tagName = 'TEXTAREA' then 
        begin 
          textField := field as IHTMLTextAreaElement; 
          textField.value := newValue; 
        end; 
      end; 
end; 
3.8、Submit the form: 
procedure TMyForm.Button1Click(Sender: TObject); 
var 
document: IHTMLDocument2; 
theForm: IHTMLFormElement; 
index: integer; 
begin 
   document := TWebBrowser.Document as IHTMLDocument2; 
   theForm := GetFormByNumber(document,0); 
   SetFieldValue(theForm,'name','Brian Cryer'); 
   theForm.submit; 

4、WebBrowser. OleObject. Document. Frames: 
.Length 文档中的框架数目。 .Item(0) 第一个框架。 .Item(0).Document 第一个框架内的文档. .Item(0).Document.URL 第一个框 架的的URL。. 
Document 相当于: 
(WebBrowser.Document as IHTMLDocument2).Frames 

例:得到关于作为IHTMLWindow2或IHTMLDocument2框架的信息: 
var 
document: IHTMLDocument2; 
ole_index: OleVariant; 
doc_all: IHTMLElementCollection; 
frame_dispatch: IDispatch; 
frame_win: IHTMLWindow2; 
frame_doc: IHTMLDocument2; 
begin 
document := WebBrowser.Document as IHTMLDocument2; 
ole_index := 0; 
frame_dispatch := document.Frames.Item(ole_index); 
if frame_dispatch <> nil then 
begin 
      frame_win := frame_dispatch as IHTMLWindow2; 
      frame_doc := frame_win.document; 
      . 
      . 

5、WebBrowser. OleObject. Document. Images: 
文档内的图像数组。 
.Length 文档中图像的数量。 .Item(0)第一个图像。 .Item(0).Src 第一个图像的地址。 


6、WebBrowser. OleObject. Document. Links

表示所有链接的数组(像 "<a href...>"元素).

.Length 链接的数量 .Item(0) 第一个链接。 .Item(0).href 第一个链接的地址。 .Item(0).TagName 第一个链接的类型名称。 对于链接来讲TagName通常为 'A'.

Document 相当于:

var

htmlDoc: IHTMLDocument2; 
allLinks: IHTMLElementCollection; 
firstLink: IHTMLElement; 
url: String; 
begin 
htmlDoc := WebBrowser.Document as IHTMLDocument2; 
allLinks := htmlDoc.Links; 
firstLink := allLinks.Item(0,'') as IHTMLElement; 
url := firstLink.toString; 
原创粉丝点击