网页代码:<SELECT id=fy οnchange=TouchRefresh(1) name=fy> <OPTION selected value=15>每頁顯示15筆</OPTION> <OPTION value=30>每頁顯示30筆</OPTION> <OPTION value=60>每頁顯示60筆</OPTION></SELECT>
1.webbrowser执行网页中的JS函数
如果要执行TouchRefresh(1)函数:
webbrowser1.oleobject.document.parentwindow.ExecScript(‘TouchRefresh(1)’,'Javascript');
webbrowser执行自己的JS函数: wb1.OleObject.document.parentwindow.ExecScript('alert(''自定义函数');','javascript');
方法2:
wb1.Navigate('javascript:alert(''执行成功!'');');
2.webbrowser选择下拉列表
var
ole : olevariant
begin
ole := webbrowser1.oleobject.document.GetElementByID('fy');
ole.value := 60; //下拉框用value,复选框和多选框用checked:='true'
ole.onchange; //调用onchange事件
end;
3.webbrowser按照Form提交(针对提交按钮无id和name的情况)
网页代码:
<FORM id=loginForm οnsubmit="return validate_form()" method=post action=/pass/serviceLoginAuth2><INPUT id=passToken type=hidden name=passToken>
<INPUT style="DISPLAY: none" id=user class="input_kuang item errortip" name=user>
<INPUT style="DISPLAY: none" id=pwd class="input_kuang item errortip" value="" type=password name=pwd>
<DIV class="sub_login flt_l"><INPUT class=no_bg value=登入 type=submit></DIV>
提交方法:
webbrowser1.OleObject.document.forms.item('loginForm').submit;
4.webbrowser去掉滚动条
在download事件中: webbrowser1.oleobject.Document.body.Scroll := 'no'; webbrowser1.oleobject.Document.documentElement.style.overflow := 'hidden';
webbrowser控制滚动条滚动: webbrowser1.OleObject.document.parentwindow.scrollby(0,100);//第一个参数横向,第二个竖向 scrollby滚动多少 webbrowser1.OleObject.document.parentwindow.scrollto(0,1000);// scrollto滚动到某个位置
5.webbrowser屏蔽鼠标右键 TEmbeddedWB设置DisabledPopupMenuItems属性
webbrowser1.DisabledPopupMenuItems := [rcmDefault,rcmImage,rcmControl,rcmTable,rcmSelText,rcmAnchor,rcmUnKnown,rcmImageArt,rcmImgDynSrc,rcmDebug,rcmAll]
6.webbrowser屏蔽网页图片 TEmbeddedWB设置DownloadImages属性
webbrowser1.DownloadOptions := [];
7.webbrowser按照元素的属性获取元素
function GetDocLinkByHref(doc : IHTMLDocument2;str_url:string):IHTMLAnchorElement;
var i : Integer; label_iDispatch : iDispatch; label_element : IHTMLAnchorElement; //引用mshtml begin Result := nil;
doc := webbrowser1.document as Ihtmldocument2; if not Assigned(doc) then Exit; for i:=0 to doc.links.length-1 do begin label_iDispatch := doc.links.item(i,0); label_element := doc.links.item(I,0) as IHTMLAnchorElement; if (AnsiPos(str_url,(doc.links.item(I,0) as IHTMLElement).getAttribute('tag',0))>0) then begin Result := label_element; Break; end; end; end;
8.webbrowser TEmbededwb屏蔽弹窗并捕获弹窗信息 //在 TEmbededwb的OnShowMessage事件中处理
procedure TForm1.wb1NavigateComplete2(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant); VAR str_url : string; begin Wb1.OleObject.document.parentWindow.execScript('window.alert=null;','JavaScript'); Wb1.OleObject.document.parentWindow.execScript('window.open=null;','JavaScript'); Wb1.OleObject.document.parentWindow.execScript('window.close=null;','JavaScript'); Wb1.OleObject.document.parentWindow.execScript('window.showModalDialog=null;','JavaScript'); Wb1.OleObject.document.parentwindow.execscript('function confirm() {return true;}','Javascript'); end;
function TForm1.webbrowser1ShowMessage(Sender: TObject; HWND: Cardinal; lpstrText, lpstrCaption: PWideChar; dwType: Integer; lpstrHelpFile: PWideChar;
dwHelpContext: Integer; var plResult: Integer): HRESULT; begin plResult := IDOK; //屏蔽alert\open\close等弹窗 Result := S_OK; //屏蔽confirm确认框,并点击确认 mmo1.Lines.Add(lpstrText); //lpstrText即为捕获到的弹窗内容 end;
//强制Webbroser使用IE8内核 SetWBCoreIE8(8000);
procedure SetWBCoreIE8(i_value:Integer);
var str_exeName : string; reg : TRegistry; begin try str_exeName := RightFromStr(Application.ExeName,'\',False); reg := TRegistry.Create; reg.RootKey := HKEY_LOCAL_MACHINE; if reg.OpenKey('Software\Microsoft\Internet Explorer\Main\FeatureControl', False) then begin if not reg.KeyExists('FEATURE_BROWSER_EMULATION') then reg.CreateKey('FEATURE_BROWSER_EMULATION'); end; reg.CloseKey; if reg.OpenKey('Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION', False) then begin reg.WriteInteger(str_exeName,i_value); // if not reg.ValueExists(str_exeName) then // begin // reg.WriteInteger(str_exeName,i_value); // end; end; reg.CloseKey; reg.Free; except end; end;
最后奉献以前收集的关于WB的文章链接:
Delphi Webbrowser
支持原创,转载请注明出处!