获取Html源码
uses mshtml;
var
iall : IHTMLElement;
begin
if Assigned(WebBrowser1.Document) then
begin
iall := (WebBrowser1.Document AS IHTMLDocument2).body;
while iall.parentElement <> nil do
begin
iall := iall.parentElement;
end;
memo1.Text := iall.outerHTML;
end;
end;
模拟登录网站
procedure TForm1.Button2Click(Sender: TObject);
Var
input: OleVariant;
Doc: IHTMLDocument2;
UserEdt, UserPwd: IHTMLElement;
i: Integer;
begin
Doc := WebBrowser1.Document as IHTMLDocument2;
input := Doc.all.item('switch_login', 0); //模拟点击按钮
input.click;
UserEdt := Doc.all.item('u', 0) as IHTMLElement;
UserPwd := Doc.all.item('p', 0) as IHTMLElement;
UserEdt.setAttribute('Value', Edit1.Text, 0);
UserPwd.setAttribute('Value', Edit2.Text, 0);
input := Doc.all.item('login_button', 0);
input.click;
end;
((WebBrowser1.Document as IHTMLDocument2).body as HTMLBody).scroll := 'no';
(WebBrowser1.Document as IHTMLDocument2).body.style.border := '0';
(WebBrowser1.Document as IHTMLDocument2).body.style.borderStyle := 'none';
(WebBrowser1.Document as IHTMLDocument2).body.style.margin := '0';
(WebBrowser1.Document as IHTMLDocument2).body.style.padding := '0';
(WebBrowser1.Document as IHTMLDocument2).body.style.overflow := 'hidden';
//DocumentComplete事件中加入下列代码即可
//去掉滚动条只对某些网页起作用,比如百度的页面就不知道为什么去不掉。
//下面是用Webbrowser显示图片时去掉边框和滚动条的方法,也适用于部分网页,实际上就是设置网页CSS。
WebBrowser1.Navigate('javascript:document.getElementById("Test").click();');
//Delphi调用WebBrowser中JavaScript代码的方法
procedure TForm1.Button1Click(Sender: TObject);
Var
I, Cnt: integer;
begin
Cnt := WebBrowser1.OleObject.Document.GetElementsByTagName('A').Length;
for I := 0 to Cnt - 1 do
Begin
If WebBrowser1.OleObject.Document.GetElementsByTagName('A').Item(I)
.InnerHtml = '活跃度' Then
Begin
WebBrowser1.OleObject.Document.GetElementsByTagName('A').item(5).Click;
// memo1.Lines.Add(WebBrowser1.OleObject.document.getElementsByTagName('A').item(i).innerHTML);
End;
End;
End;
//遍历A元素,通过得到文字后进行模拟点击
WebBrowser1.GoHome; //到浏览器默认主页
WebBrowser1.Refresh; //刷新
WebBrowser1.GoBack; //后退
WebBrowser1.GoForward; //前进
WebBrowser1.Navigate('...'); //打开指定页面
WebBrowser1.Navigate('about:blank'); //打开空页面
procedure TForm1.Button1Click(Sender: TObject);
begin
WebBrowser1.Navigate(
'about:<head><title>标题</title><body bgcolor=#ff0000>' +
'<form method="POST" action="http://del.cnblogs.com">' +
'<input type="submit" value="提交" id="btnID" name="btnName">' +
'</form></body>');
Button2.Enabled := True;
Button3.Enabled := True;
Button4.Enabled := True;
end;
{假如知道按钮名称, 譬如是: btnName}
procedure TForm1.Button2Click(Sender: TObject);
begin
WebBrowser1.OleObject.document.all.item('btnName').click;
//WebBrowser1.OleObject.document.all.item('btnName', 0).click;
FormCreate(nil);
end;
{假如知道按钮的 ID, 譬如是: btnID}
procedure TForm1.Button3Click(Sender: TObject);
begin
WebBrowser1.OleObject.document.getElementByID('btnID').click;
FormCreate(nil);
end;
{假如只知道是第几个按钮, 譬如是第一个}
procedure TForm1.Button4Click(Sender: TObject);
begin
WebBrowser1.OleObject.document.getElementsByTagName('input').item(0).click;
FormCreate(nil);
end;
end.
//获取网页源代码
var
s: string;
begin
s := WebBrowser1.OleObject.document.body.innerHTML; //body内的所有代码
s := WebBrowser1.OleObject.document.body.outerHTML; //body内的所有代码, 包含body标签
s := WebBrowser1.OleObject.document.documentElement.innerHTML; //html内的所有代码
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
WebBrowser1.Navigate('www.baidu.com');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Key: string;
doc, sr: OleVariant;
begin
if WebBrowser1.Busy = True then
Begin
Exit;
End;
Key := 'gfuchao'; // 待查找的字符串,你自己定义吧
doc := WebBrowser1.Document;
if VarIsEmpty(doc) then
Exit;
if VarIsEmpty(doc.selection) then
sr := doc.body.createTextRange
else
begin
sr := doc.selection.createRange;
sr.collapse(True);
sr.moveStart('character', 1);
end;
if sr.findText(Key, 1, 0) then
sr.select // findText为True就表示含有关键字,你自己处理吧
else
ShowMessage('已收缩到文档末尾!');
sr := Unassigned;
end;