使用VBA登录网站 [英] Login to a website using VBA

查看:217
本文介绍了使用VBA登录网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更熟悉Excel宏(VBA)并寻找一种方法来自动化IE登录网站并进入某个页面。
网站网址: https://www.mast-technicalservices.com/ecp /index2.jsp 并需要填写登录详细信息并单击继续。

Im a fresher to Excel Macros (VBA) and looking for a way to automate IE to login to a website and proceed to a certain page. website URL : https://www.mast-technicalservices.com/ecp/index2.jsp and need to fill the login details and click continue.

Sub WebsiteLogIn()

Dim nURL As String
Dim UNElementID As String
Dim UserName As String
Dim PWElementID As String
Dim Password As String
Dim SIElementID As String


Set LoginData = ThisWorkbook.Sheets("Sheet1")
Set nURL = "https://www.mast-technicalservices.com/ecp/index2.jsp"
Set UNElementID = "ecp_param_userId"
Set UserName = LoginData.Cells(1, "B").Value
Set PWElementID = "ecp_param_password"
Set Password = LoginData.Cells(2, "B").Value
Set SIElementID = "imageField2"

Dim IE              As Object
Dim IEPage          As Object
Dim IEPageElement   As Object


    'Create a new Internet Explorer instance, make it visible and maximize its window and navigate to url.
    On Error Resume Next
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    ShowWindow IE.hwnd, SW_MAXIMIZE

    IE.navigate URL

    Set IEPage = IE.document

    'setthe UserName text box using the element ID.
    Set IEPageElement = IEPage.getElementById(UNElementID)

    'set the Password text box using the element ID.
    Set IEPageElement = IEPage.getElementById(PWElementID)


    'set the Continue button using the element ID.
    Set IEPageElement = IEPage.getElementById(SIElementID)


End Sub


推荐答案

这个有用:

Sub login()

    Const Url$ = "https://www.mast-technicalservices.com/ecp/index2.jsp"

    Dim UserName As String, Password As String, LoginData As Workbook
    Set LoginData = ThisWorkbook.Worksheets("Sheet1")
    UserName = LoginData.Cells(1, "B").Value
    Password = LoginData.Cells(2, "B").Value

    Dim ie As Object
    Set ie = CreateObject("InternetExplorer.Application")

    With ie

        .navigate Url
        ieBusy ie
        .Visible = True

        Dim oLogin As Object, oPassword As Object
        Set oLogin = .document.getElementsByName("ecp_param_userId")(0)
        Set oPassword = .document.getElementsByName("ecp_param_password")(0)

        oLogin.Value = UserName
        oPassword.Value = Password
        .document.forms(0).submit

    End With

End Sub

Sub ieBusy(ie As Object)
    Do While ie.Busy Or ie.readyState < 4
        DoEvents
    Loop
End Sub

另一个子: ieBusy 将确保在您尝试操作之前网页已完全加载。

The other sub: ieBusy will ensure the webpage is fully loaded prior to you attempting to manipulate it.

这篇关于使用VBA登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆