首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

访问TWEBBROWSER里的控件解决思路

2012-03-26 
访问TWEBBROWSER里的控件以下代码可以访问到所有的input标签控件,但像Select和TextArea等要怎么进行访问和

访问TWEBBROWSER里的控件
以下代码可以访问到所有的input标签控件,但像Select和TextArea等要怎么进行访问和赋值呢?  

  try 
  HTMLDocument := IHTMLDocument2(IE.Document); 
  // HTMLDocument.links 
  ElementCollection := HTMLDocument.Get_All; 
  for k := 0 to ElementCollection.Length - 1 Do 
  begin 
  vk := k; 
  Application.ProcessMessages; 
  Dispatch := ElementCollection.item(vk, 0); 
  HTMLElement := ElementCollection.item(vk,0) As IHtmlElement; 
  { 
  // 遍历文字链接 
  if SUCCEEDED(Dispatch.QueryInterface(IHTMLElement, HTMLElement)) Then 
  begin 
  if HTMLElement.tagName = "A " then 
  begin 
  Res := "内部文字为了: " + HTMLElement.innerText + "内部标签为: " + HTMLElement.tagName; 
  MemoResult.Lines.Add(Res); 
  MemoResult.Lines.Add( " "); 
  if HTMLElement.innerText = "信仰爱情项链 " then 
  begin 
  ShowMessage( "找到信仰爱情了. "); 
  HTMLElement.click; 
  end; 
  end; 
  end; 
  } 
  if SUCCEEDED(Dispatch.QueryInterface(IHTMLInputElement, HTMLInputElement)) Then 

  with HTMLInputElement Do 
  begin 
  if (UpperCase(Type_) = "TEXT ") and (UpperCase(Name) = "USERNAME ") then 
  Value := "haicao ";  
  if (UpperCase(Type_) = "PASSWORD ") and (UpperCase(Name) = "PASSWORD ") then 
  Value := "654321 "; 
  if (UpperCase(Type_) = "SELECT ") and (UpperCase(Name) = "COOKIEDATE ") then 
  Value := "2 "; 
  Res := "类型: " + type_ + " 控件名称: " + name + " SRC= " + src + " 值: " + value; 

  MemoResult.Lines.Add(Res); 
  MemoResult.Lines.Add( " "); 
  //if (UpperCase(Type_) = "RADIO ") and 
  // (UpperCase(Name) = "R1 ") and 
  // (UpperCase(Value) = IntToStr(ID)) Then 
  // begin 
  // Checked := true; 
  // HTMLInputElement.disabled := True; 
  // HTMLInputElement.readOnly := True; 
  // end;  
  //if (UpperCase(Type_) = "SUBMIT ") and (UpperCase(Value) = "提 交 ") Then 
  // form.submit; 
  end; 
  end; 
  except 
  Result := false; 
  Exit; 
  end; 
  Result := true; 




[解决办法]
uses mshtml;

procedure TForm1.FormCreate(Sender: TObject); 
begin 
webbrowser.Navigate( "C:\Documents and Settings\Administrator\桌面\index.htm "); 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
var 
Doc3: IHTMLDocument3; 
e: mshtml.IHTMLSelectElement; 
begin 
Doc3:=self.WebBrowser1.Document as IHTMLDocument3; 
e:=doc3.getElementById( "id,select的id属性 ") as IHTMLSelectElement;//用id属性准确定位到一个指定的select上去 
if e < >nil then 
e.value:= "中国 "; 
end;

select的html这样写: <select id= "selectcountry " >......... </select > 

textarea请举一反三

热点排行