WebBrowser与MSHTML实现点击操作,与自己鼠标点击有差异,求解

cilys 2010-01-01 03:19:03
用途:程序用WebBrowser打开CSDN主页http://www.csdn.com,点击一个链接,标签是<a href="http://tech.sina.com.cn" target="blank">新浪科技</a>

实现:
1.手动点击的话,该链接就在当前WebBrowser窗口中链接到http://tech.sina.com.cn
2.Delphi程序实现,HtmlElement对象执行click操作后,弹出了360浏览器(我这默认的浏览器)进入目标网页
3.C#中,HtmlElement对象执行InvokeMember("click")操作,实现与1即手动的效果。

下面是Delphi的代码:


procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
var
doc: IHTMLDocument2;
em: IHtmlElement;
colLink: IHTMLDocument2;
begin
if WebBrowser1.Application=pDisp then begin
showmessage('Web download is over.');

doc := WebBrowser1.Document as IHTMLDocument2;
if doc <> nil then begin
em := GetHtmlElement(doc, 'links','innerText','新浪科技');
//em.setAttribute('target', '_self', 0);//无效果
if em<>nil then
em.click; //点击
end
else begin
showmessage('Html doc is null.');
end;
end;
end;


Delphi我初学,写多了点只是想把问题描述清楚。

问题:
1.让程序实现手动鼠标点击的效果,不弹出浏览器,请指点一下应该如何修改?
2.setAttribute为什么没效果?
3.HTML标签中明明有target="blank",为什么手动鼠标点击时没有弹窗。这个有点费解。

...全文
497 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
cilys 2010-01-05
  • 打赏
  • 举报
回复
继续请人指点下,谢谢了
wuhenghuan 2010-01-05
  • 打赏
  • 举报
回复
学习了
dongmei757 2010-01-03
  • 打赏
  • 举报
回复
学习
pilicat 2010-01-02
  • 打赏
  • 举报
回复
学习了。
cilys 2010-01-02
  • 打赏
  • 举报
回复
他源码里就是 target="blank" 弹窗的啊 普通的浏览器都是正常弹的,用webbrowse手动点反而不弹了,有点诡异
iseekcode 2010-01-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cilys 的回复:]
好,多谢

那鼠标直接点的时候,为什么没有弹窗呢?这个不解
[/Quote]

html源码里可以设置是否在当前窗口或新窗口打开。
hjkto 2010-01-01
  • 打赏
  • 举报
回复
标记
cilys 2010-01-01
  • 打赏
  • 举报
回复
好,多谢

那鼠标直接点的时候,为什么没有弹窗呢?这个不解
zhuang_bx 2010-01-01
  • 打赏
  • 举报
回复
procedure TfrmAdBrowser.WebBrowser1NewWindow2(Sender: TObject;
var ppDisp: IDispatch; var Cancel: WordBool);
begin
ppDisp:=WebBrowser2.Application;//加一个WEBBrowser2 这样在DELPHI中就不弹出来了

end;
cilys 2010-01-01
  • 打赏
  • 举报
回复
GetHtmlElement是自定义函数,取出一个IHtmlElement对象。我估计和这关系不大,代码也贴出来


{
自定义函数,获取满足条件的标签
tagName:标签名 关键字:all,links,images,applets,forms,anchors,scripts,embeds,plugins,parentWindow
atName: 属性名 关键字:innerText(暂时只处理这个),
atValue:属性值
}
function TForm1.GetHtmlElement(doc:IHTMLDocument2; tagName,atName,atValue:string):IHtmlElement;
var
col: IHTMLElementCollection;
em: IHtmlElement;
i: Integer;
begin
if doc <> nil then begin
//判断标签名
if tagName = '' then
col := doc.all As IHTMLElementCollection
else if tagName = 'all' then
col := doc.all As IHTMLElementCollection
else if tagName = 'links' then
col := doc.links As IHTMLElementCollection
else if tagName = 'images' then
col := doc.images As IHTMLElementCollection
else if tagName = 'applets' then
col := doc.applets As IHTMLElementCollection
else if tagName = 'forms' then
col := doc.forms As IHTMLElementCollection
else if tagName = 'anchors' then
col := doc.anchors As IHTMLElementCollection
else if tagName = 'scripts' then
col := doc.scripts As IHTMLElementCollection
else if tagName = 'embeds' then
col := doc.embeds As IHTMLElementCollection
else if tagName = 'plugins' then
col := doc.plugins As IHTMLElementCollection
else if tagName = 'parentWindow' then
col := doc.parentWindow As IHTMLElementCollection
else
col := doc.all.tags(tagName) As IHTMLElementCollection;

//判断标签的id及name,如果都没有则返回,查询条件不足;
if (atName='') or (atValue='') then begin
showmessage('GetHtmlElement error:查询条件不足');
exit;
end;

//循环标签容器,开始获取满足条件的第一个元素
for i := 0 to col.length - 1 do begin
em := col.item(i,0) as IHtmlElement;
if atName='innerText' then begin
if atValue=em.innerText then begin
result := em;
end;
end
else if atValue=em.getAttribute(atName,0) then begin
result := em;
exit;
end;
end;
end else begin
showmessage('GetHtmlElement error:Html doc is null.');
exit;
end;
end;

5,927

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧