vba - 使用VBA登录网站

标签 vba internet-explorer browser-automation

我是 Excel 宏 (VBA) 的新手,正在寻找一种使 IE 自动登录网站并进入特定页面的方法。 网址:https://www.mast-technicalservices.com/ecp/index2.jsp并需要填写登录详细信息,然后单击继续。

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 Worksheet
    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 将确保网页在您尝试操作之前已完全加载。

关于vba - 使用VBA登录网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48919491/

相关文章:

excel - 重播宏更改选项卡颜色时下标超出范围

java - 在 Excel 中以编程方式编辑宏

jQuery IE 追加 ul 不会应用 css 样式

css - 最后一个字符无法显示在 IE9 中的圆形输入字段中

html - 背景在 IE8 中不工作

python - 如何从 url 列表中查找有多少 url 正在运行

java - 如何从自动完成文本框的自动建议中获取文本?

vba - 使用 EntryID、StoreID 和/或 PR_ENTRYID 打开 Outlook 邮件项目

vba - 无法将定义的变量加一

java - 如何使用selenium webdriver在FB中打印在线好友姓名?