分享

VBA-填写网页表单的方法

 永远的渴望 2013-11-24

VBA-填写网页表单的方法  

2011-08-04 12:47:32|  分类: Excel自动化 |  标签: |字号 订阅

本次介绍的方法是http://chiensq.blog.163.com/blog/static/1035628201174015765/?newFollowBlog中介绍的第二种方法控制网页

要填写网页表单,就要知道网页中各个要填写的项目名称,就需要分析网页。分析网页有好几种方法:

以:http://mial.163.com/为例

1、直接查看网页的源代码(网页右键有该功能)

在源代码中直接搜索如“用户名”,“密码”等需要填写的项目的文字。找到类似以下的代码

<LABEL class="lb for for-1">用户名</LABEL> <LABEL class="lb for for-2 for-3">手机号</LABEL> <INPUT class=ipt-t id=idInput onblur="fCheckAccount(this);fEvent('blur',this)" onmouseover="fEvent('mouseover',this)" onfocus="fEvent('focus',this)" tabIndex=1 onmouseout="fEvent('mouseout',this)" maxLength=50 name=username autocomplete="off" isfocus="false"> <SPAN class=domain>@163.com</SPAN>

将该代码分段

 <LABEL class="lb for for-1">用户名</LABEL>              --是显示“用户名”的文字的表格,并不是输入项目

 <LABEL class="lb for for-2 for-3">手机号</LABEL>   --是显示“用户名”的文字的表格,并不是输入项目

而已INPUT开始的就是需要输入的项目,发现有name=username的代码,意思是用户名就是username,这就是我们要找的。

一般都是找 ID= 或者 name = 。

 <INPUT class=ipt-t id=idInput onblur="fCheckAccount(this);fEvent('blur',this)" onmouseover="fEvent('mouseover',this)" onfocus="fEvent('focus',this)" tabIndex=1 onmouseout="fEvent('mouseout',this)" maxLength=50 name=username autocomplete="off" isfocus="false">

 2、但是项目多的话,这样找比较麻烦,也可以用VBA代码直接找

引用老狼的代码:http://blog.csdn.net/northwolves/article/details/1809109

Sub show163tags()
Dim i As Byte
With CreateObject("InternetExplorer.Application")
.Visible = True
.navigate "http://mail.163.com"
Do Until .Readystate = 4
DoEvents
Loop
On Error Resume Next
For i = 1 To 100
If Len(.Document.Forms(0).All(i).Name) > 0 Then Debug.Print "i=" & i; "  name=" & .Document.Forms(0).All(i).Name
Next
End With
MsgBox "ok"
End Sub

3、但是有的项目是没有ID和Name的,就比如163邮箱这个button即没ID又没name。

这个时候可以用到索引号,手工找就不介绍了,直接推荐用Ldy的分析工具

http://club./thread-377077-1-1.html

 

参考文章

http://club./thread-719242-1-1.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多