分享

VBA,VB 自动登录网站

 精神360 2022-04-03

很多时候大家都需要登录网站, 查询一下信息。可不可以在office中登录呢?可以,用VBA即可。

需要添加到引用

  Microsoft Internet Controls

  Microsoft HTML Object Library

Sub loginRenRen()

    Dim ie As InternetExplorer

    Dim doc As HTMLDocument

    Set ie = New InternetExplorer

    ie.Navigate "http://www.renren.com/"

    ie.Visible = True

    'http://www.renren.com/Home.do 个人主页

    Do

      DoEvents

    Loop Until ie.ReadyState = 4 '在网页没有完全下载之前,VB 转让控制权

    If ie.LocationURL = "http://www.renren.com/Home.do" Then Exit Sub '如果已经登录,退出程序

    Set doc = ie.Document

    doc.getElementById("email").Value = "用户名"

    doc.getElementById("password").Value = "密码"

    doc.getElementById("login").Click

    doc.Close

    Set doc = Nothing

    'ie.Quit

    Set ie = Nothing

End Sub

————————————————

版权声明:本文为CSDN博主「to_gengqing」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/to_gengqing/article/details/5498804

https://blog.csdn.net/to_gengqing/article/details/5498804?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1.pc_relevant_antiscanv2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1.pc_relevant_antiscanv2&utm_relevant_index=2

=======另外文章

这里有两问题:

1、ie.Navigate "xxx_login.php"打开网页后,在网页没有载入完毕立即去获取网页控件会失败;

2、ie.document.GetElementsByName("pwuser").value获取控件中是不是Element而不是Elements.

dim ie

Set ie=CreateObject("InternetExplorer.application")

ie.Visible=true

ie.Navigate "xxx_login.php"

'等待网页载入完成

Do

Sleep 200

Loop Until IE.ReadyState = 4

ie.document.GetElementByName("pwuser").value = "用户名"

ie.document.GetElementByName("pwpwd").Value = "密码"

ie.document.GetElementByName("submit").Click追问: VBA

追答:在最前面添加语句Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

下面是例子:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub a()

Sleep 2000

MsgBox "1"

End Sub

https://blog.csdn.net/weixin_36212334/article/details/117737838

=============另篇文章

用VBA制作一个用户窗体,如果是多个网页多个用户登入,就多写几个,用按钮控制
Private Sub CommandButton1_Click()  '打开网址按钮代码
Me.WebBrowser1.Navigate "输入要打开的网址"
Action = 2
End Sub

Private Sub CommandButton2_Click() '设定用户和密码与网页嫁接
Set f = Me.WebBrowser1.Document.all("EinForm0")
f.elements("AccountId").Value = '用户名
f.elements("Password").Value = '密码
Me.WebBrowser1.Document.all("EinForm0_SubmitBtn").Click
Action = 3
End Sub

来源:http://www./thread-222297-1-1.html

===============另篇文章

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">     <tr>   <td align="center" valign="middle"><table width="80%" height="94" border="0" align="center" cellpadding="0" cellspacing="1" bordercolor="#cccccc" class="tab2">  <tr> <td height="30" colspan="2" align="center" class="font14pxB1"> <span id="myLabel" class="error1"></span>            </td>  </tr>                              <tr> <td width="57%" height="30" align="right" class="font14pxB1">用 户 名:                                                          <input name="RealName" type="text" value="hdtianyh" id="RealName" style="width:170px;" />      </td>                                                    <td width="43%" align="left" class="font14px1">     请输入您的用户名、密码,             <span id="RequiredFieldValidator" style="color:#FF6600;display:none;"><br><img src=/images/warn.gif>请填写用户名!<br></span></td>  </tr>                                     <tr align="right">   <td height="30" class="font14pxB1">密         码:                                                                                               <input name="Password" type="password" id="Password" value="000000" style="width:170px;" />    </td>

上面是源文件,下面是方法

解决了    应该把 Set iea = CreateObject("InternetExplorer.Application")
放在 iea.navigate "http://172.17.136.114/", False就可以了。

Private Sub Workbook_Open()
Dim iea
Set iea = CreateObject("InternetExplorer.Application")
iea.navigate "http://172.17.136.114/", False
Do Until iea.ReadyState = 4
            DoEvents
        Loop
iea.document.all("RealName").Value = "hdqinyj"
iea.document.all("Password").Value = "000000"
iea.document.all("Button1").Click

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

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多