2

我使用的是Delphi XE2 + TWebbrowser,网页源代码如下:

<html>
<head>
<title>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
var loaded = 0;function ScriptLoaded() { ++loaded; if(loaded == 2) ARSystemInit(); }
var ReportSelForm="ReportSelection", RelContextPath='../../../../',AbsContextPath=null;</script>
<link rel="stylesheet" href="../../../../resources/stylesheets/7.1.00 Patch 009 201002230034/ARSystem.css">
<link rel="stylesheet" href="../../../../resources/stylesheets/7.1.00 Patch 009 201002230034/ARSystem_zh.css">
<script type="text/javascript" src="../../../../resources/javascript/7.1.00 Patch 009 201002230034/LocalizedMessages_zh_CN.js" defer></script>
<script type="text/javascript" src="../../../../resources/javascript/7.1.00 Patch 009 201002230034/locale/zh_CN.js" defer></script>
<script type="text/javascript">function DVFsf(){return "\x3cHTML\x3e\x3c/HTML\x3e";} function DVFol(){}</script>
<script type="text/javascript" src="../../../../resources/javascript/7.1.00 Patch 009 201002230034/ClientCore.js" defer></script>
<script type="text/javascript" src="form.js/f7a68ba8.js" defer></script>
<script type="text/javascript">document.write('\x3cscript src="userdata.js?winname=ar05UltraProcess%3AITIL_Incident'+encodeURIComponent(name)+'" defer\x3e\x3c/'+'script\x3e');</script>
</head>
<body onload="ScriptLoaded&#40;&#41;" draghandler="BodyDrag" style="display:none">
<div id="Toolbar"><div class="TBTopBarStatus">当前模式: <span class="TBTopBarStatusMode"></span></div><a class="btn TBTopBarBox" href="javascript:"><img class="tbright" src="../../../../resources/images/tb_right.gif" alt="显示工具栏" /><img class="tbdown" src="../../../../resources/images/tb_down.gif" alt="隐藏工具栏" /></a><table cellpadding=0 cellspacing=0 class="Toolbar"><tbody><tr><td nowrap class="TBGroup TBGroup0">
<a class="searchsavechanges btn btn3d tbbtn" href="javascript:" style="position:static">
<div id="TBsearchsavechanges">搜索</div>
</a>
</td>
<td nowrap class="TBGroup TBGroup1">
<a class="newsearch btn btn3d tbbtn" href="javascript:" style="position:static">
<div id="TBnewsearch"><img src="../../../../resources/images/tb_search.gif" alt="新搜索" /> 新搜索&lt;/div>
</a>
<a class="newrequest btn btn3d tbbtn" href="javascript:" style="position:static">
<div id="TBnewrequest"><img src="../../../../resources/images/tb_request.gif" alt="新请求" /> 新请求&lt;/div>
</a>
<a class="modifyall btn btn3d tbbtn" href="javascript:" style="position:static">
<div id="TBmodifyall"><img src="../../../../resources/images/tb_modall.gif" alt="全部修改" /> 全部修改</div>
</a>
</td>
<td nowrap class="TBGroup TBGroup2">
<a class="savedsearches btn btn3d tbbtn" href="javascript:" style="position:static">
<div id="TBsavedsearches"> 多项搜索</div>
</a>
<a class="advancedsearch btn btn3d tbbtn" href="javascript:" style="position:static">
<div id="TBadvancedsearch">高级搜索</div>
</a>
<a class="clear btn btn3d tbbtn" href="javascript:" style="position:static">
<div id="TBclear">清除</div>
</a>
<a class="settodefaults btn btn3d tbbtn" href="javascript:" style="position:static">
<div id="TBsettodefaults">设置为默认值&lt;/div>
</a>
<a class="statushistory btn btn3d tbbtn" href="javascript:" style="position:static">
<div id="TBstatushistory">状态历史记录&lt;/div>
</a>
</td>
<td nowrap class="TBGroup TBGroup3">
<a class="logout btn btn3d tbbtn" href="javascript:" style="position:static">
<div id="TBlogout">注销</div>
</a>
<a class="help btn btn3d tbbtn" href="javascript:" style="position:static">
<div id="TBhelp">帮助</div>
</a>
<a class="home btn btn3d tbbtn" href="javascript:" style="position:static">
<div id="TBhome">主页</div>
</a>
</td>
</tr>
</tbody>
</table>
</div>
<div id="ResultsList">
<div arid=1020 artype="Table" ardbn="CustomResultsList" class="arfid1020 ardbnCustomResultsList" style="top:0&#59; left:0&#59; width:0&#59; height:0&#59;visibility:hidden&#59;z-index:0&#59;" arcols="2147483647" arcolws="1575" arcoldlen="254" ardrill=1 arserver="@" arschema="@" armaxrows=0 ardtype=1 arcsize=0 arfixhdr=1 arselrows=0 arselinit=0 arselrefresh=0 arautofit=0 arsort="" arqual="{qual:&quot;&quot;, ids:[],extids:[]}"><img src="../../../../resources/images/menu_down.gif" alt="" class="TableSortImgUp" arindom=1 /><img src="../../../../resources/images/menu_up.gif" alt="" class="TableSortImgDown" arindom=1 /><div class='TableHdr'><table class='TableHdr' cellpadding=0 cellspacing=0><tbody><tr><td nowrap valign=middle class='TableHdrL'></td><td nowrap valign=middle class='TableHdrR'></td></tr></tbody></table></div><div class='TableInner'></div><div class='TableFtr'><table class='TableFtr' cellpadding=0 cellspacing=0><tbody><tr><td nowrap valign=middle class='TableFtrL'></td><td nowrap valign=middle class='TableFtrR'></td></tr></tbody></table></div></div>
</div>
<div id="Splitter" draggable=1 dragoriginal=1>
</div>
<div id="FormContainer" arcs=1 style="background-color:">
<div style="top:0&#59;left:0&#59;width:1173&#59;height:869">&nbsp;</div>
<div arid=700000002 artype="Char" ardbn="BaseName" class="df  dfro arfid700000002 ardbnBaseName" style="z-index:994&#59;top:2&#59; left:9&#59; width:100&#59; height:25&#59;">
<span style="top:0&#59; left:0&#59; width:100&#59; height:0&#59;"><label class="label f9" for="x-arid700000002" style="width:100px&#59;left:0px&#59;bottom:25px&#59;color:&#59;text-align:center&#59;"></label></span><textarea class="text sr f2 dat" wrap="off" id="arid700000002" cols="20" maxlen=254 style="top:0&#59; left:0&#59; width:100&#59; height:25&#59;" readonly rows=1></textarea>
</div>
<a href="javascript:" arid=700040001 artype="Control" ardbn="Btn_Chart" class="btn btnurl arfid700040001 ardbnBtn_Chart" style="top:5&#59; left:842&#59; width:54&#59; height:20&#59;visibility:hidden&#59;z-index:998&#59;background-color:transparent&#59;"><div class="btntextdiv" style="top:0&#59; left:0&#59; width:54&#59; height:20&#59;"><div class="f7" style="&#59;width:54">流 程 图&lt;/div>
</div>
</a>
<a href="javascript:" arid=809041101 artype="Control" ardbn="Btn_AddDate" class="btn btnurl arfid809041101 ardbnBtn_AddDate" style="top:5&#59; left:916&#59; width:65&#59; height:20&#59;visibility:hidden&#59;z-index:1004&#59;background-color:transparent&#59;"><div class="btntextdiv" style="top:0&#59; left:0&#59; width:65&#59; height:20&#59;"><div class="f7" style="&#59;width:65">补充资料</div>
</div>
</a>
<a href="javascript:" arid=700040078 artype="Control" ardbn="Btn_Shengchengzhishi" class="btn btnurl arfid700040078 ardbnBtn_Shengchengzhishi" style="top:31&#59; left:841&#59; width:65&#59; height:20&#59;visibility:hidden&#59;z-index:1003&#59;background-color:transparent&#59;"><div class="btntextdiv" style="top:0&#59; left:0&#59; width:65&#59; height:20&#59;"><div class="f7" style="&#59;width:65">生 成 知 识&lt;/div>
</div>
</a>
**<a href="javascript:" arid=700040002 artype="Control" ardbn="Btn_Edit" class="btn btn3d  btnd arfid700040002 ardbnBtn_Edit" style="top:32&#59; left:916&#59; width:65&#59; height:20&#59;visibility:hidden&#59;z-index:999&#59;"><div class="btntextdiv" style="top:0&#59; left:0&#59; width:65&#59; height:20&#59;"><div class="f1" style="&#59;width:65">处 理&lt;/div>
</div>
</a>**
<div arid=700000003 artype="Char" ardbn="BaseSN" class="df  dfro arfid700000003 ardbnBaseSN" style="z-index:995&#59;top:7&#59; left:592&#59; width:198&#59; height:20&#59;">
<label class="label f6" for="x-arid700000003" style="top:4&#59; left:0&#59; width:51&#59; height:16&#59;">流水号:</label><textarea class="text sr  dat" wrap="off" id="arid700000003" cols="20" maxlen=254 style="top:0&#59; left:52&#59; width:146&#59; height:20&#59;" readonly rows=1></textarea>
</div>
<div arid=700000004 artype="Char" ardbn="BaseCreatorFullName" class="df  dfro arfid700000004 ardbnBaseCreatorFullName" style="z-index:996&#59;top:7&#59; left:252&#59; width:122&#59; height:20&#59;">
<label class="label f6" for="x-arid700000004" style="top:4&#59; left:0&#59; width:60&#59; height:16&#59;">建单人名:&lt;/label><textarea class="text sr  dat" wrap="off" id="arid700000004" cols="20" maxlen=254 style="top:0&#59; left:61&#59; width:61&#59; height:20&#59;" readonly rows=1></textarea>
</div>
<div arid=806000100 artype="Char" ardbn="Status" ardcf=1 class="df  dfro arfid806000100 ardbnStatus" style="z-index:1002&#59;top:7&#59; left:110&#59; width:134&#59; height:20&#59;">
<label class="label f6" for="x-arid806000100" style="top:4&#59; left:0&#59; width:38&#59; height:16&#59;color:&#59;">状态:</label><textarea class="text sr  dat" wrap="off" id="arid806000100" cols="20" maxlen=254 style="top:0&#59; left:39&#59; width:95&#59; height:20&#59;" readonly rows=1></textarea>
</div>
<div arid=700000006 artype="Time" ardbn="BaseCreateDate" class="df  dfro arfid700000006 ardbnBaseCreateDate" style="z-index:997&#59;top:7&#59; left:386&#59; width:194&#59; height:20&#59;">
<label class="label f6" for="arid700000006" style="top:4&#59; left:0&#59; width:60&#59; height:16&#59;">登记时间:&lt;/label><input id="arid700000006" class="text  dat" type="text" style="top:0&#59; left:61&#59; width:133&#59; height:20&#59;" readonly ds=0 />
</div>
<div arid=700030001 artype="PageHolder" armaxh=22 class="PageHolder arfid700030001 ardbnPag_Main" style="top:52&#59; left:12&#59; width:973&#59; height:419&#59;background-color:white&#59;z-index:1000&#59;">
<div class="TabChildMissingBorder" style="visibility:hidden&#59;top:22&#59;width:973">
</div>
<div style="visibility:hidden&#59;height:22&#59;width:973" class="OuterTabsDiv">
<div class="ScrollingTab" style="position:relative&#59;float:left&#59;">
<span class="TabLeftRounded">&nbsp;</span>
<span class="Tab"><a href="javascript:" class="btn" id="prevTab">&lt;&lt;</a>
</span>
<span class="TabRightRounded">&nbsp;</span>
</div>
<div class="TabsViewPort" style="position:relative&#59;overflow:hidden&#59;float:left&#59;width:5000&#59;height:22">
<div style="overflow:visible&#59;float:left&#59;width:5000&#59;top:0&#59;left:0">
<dl class="OuterOuterTab">
<dd class="OuterTab" artabid=700030002><span class="TabLeftRounded">&nbsp;</span>
<span class="Tab"><a href="javascript:" class="btn f1">基  本&lt;/a>
</span>
<span class="TabRight">&nbsp;</span>
</dd>
<dd class="OuterTab" artabid=806842007><span class="TabLeft">&nbsp;</span>
<span class="Tab"><a href="javascript:" class="btn f1">事件信息</a>
</span>
<span class="TabRight">&nbsp;</span>
</dd>
<dd class="OuterTab" artabid=700030094><span class="TabLeft">&nbsp;</span>
<span class="Tab"><a href="javascript:" class="btn f1">流程记录</a>
</span>
<span class="TabRightRounded">&nbsp;</span>
</dd>
</dl>
</div>
</div>

看页面源码 BOLD 部分,是网页上的一个 BUTTON(arid=700040002 artype="Control" ardbn="Btn_Edit" )。当这个页面满载时,我想在 delphi 中按下按钮。

4

2 回答 2

2

这应该这样做:

procedure ClickElementByContent(Document : IDispatch; TagFilter : WideString; Content: WideString);

var Doc   : IHTMLDocument3;
    El    : IHTMLElement;
    Elements : IHTMLElementCollection;
    Index    : Integer;

begin
 Doc := Document as IHTMLDocument3;
 if Assigned(Doc) then
  begin
   Elements := Doc.getElementsByTagName(TagFilter);
   for Index := 0 to Pred(Elements.length) do
    begin
     El := Elements.item(EmptyParam, Index) as IHTMLElement;
     if ContainsText(El.innerText, Content) then
      begin
       El.Click;
       Break;
      end;
    end;
  end;
end;

如何使用:

procedure clickthebutton;
begin
 ClickElementByContent(Webbrowser1.Document, 'DIV', 'DEAL');
end;
于 2012-07-12T14:49:31.413 回答
0
procedure TTrackITOrderForm.Btn_DealClick(Sender: TObject);
var
Document : IHTMLDocument2 ;
ButtonDiv , temp : IHTMLElement ;
ElementCount , I : Integer ;
begin
Document := wb1.Document as IHTMLDocument2 ;
ElementCount := Document.all.length ;
for i := i to ElementCount-1 do
begin
temp := Document.all.item(i,'') as IHTMLElement ;
lst1.Items.Add(temp.id+'-->'+temp.innerText)  ;
if (temp.tagName = 'DIV') and (temp.innerText = '处理') then
begin
ShowMessage(temp.innerText);
ButtonDiv := temp ;
Break ;
end;
end;
if ButtonDiv <> nil then
ButtonDiv.click ;
end;
于 2012-07-19T03:09:17.657 回答