在网页上输入用户名和密码

时间:2022-09-11 19:21:36

Using Excel VBA, I'm trying to login to a website.

使用Excel VBA,我正在尝试登录一个网站。

This is my code:

这是我的代码:

Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Website_Login()

Dim oHTML_Element As IHTMLElement
Dim sURL As String

sURL = "https://www.domain.com/login.html"

Set oBrowser = New InternetExplorer
'oBrowser.Silent = True
'oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True

Do
Loop Until oBrowser.ReadyState = READYSTATE_COMPLETE

Set HTMLDoc = oBrowser.Document

With HTMLDoc
  HTMLDoc.getElementById("username").Value = "user"
  HTMLDoc.getElementById("password").Value = "password"
End With

For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
  oHTML_Element.Click
  Exit For
Next

oBrowser.Quit

End Sub

The Javascript instructions in this code are working. If I go to the page and enter them, they work. But when I run the code, IE opens but it doesn't enter the user/pass and click the button.

该代码中的Javascript指令正在工作。如果我进入页面并输入它们,它们就会工作。但当我运行代码时,IE会打开,但它不会输入用户/传递并单击按钮。

Any help, please?

任何帮助吗?

1 个解决方案

#1


1  

I just tested this code and it worked.

我刚刚测试了这段代码,它成功了。

The only difference is that I used Late Binding (too lazy to set all references) and I added While .Busy to the Loop to wait for the URL to load. I also changed input to button in the button click loop.

惟一的区别是,我使用了后期绑定(太懒了,无法设置所有引用),然后我添加了。我还在按钮单击循环中将输入改为按钮。

Dim HTMLDoc As Object 'HTMLDocument
Dim oBrowser As Object 'InternetExplorer

Sub Website_Login()

Dim oHTML_Element As Object 'IHTMLElement
Dim sURL As String

sURL = "https://www.mypage.com/login.html"
Set oBrowser = CreateObject("InternetExplorer.Application") 'New InternetExplorer
'oBrowser.Silent = True
'oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True

With oBrowser
    Do While .Busy Or .ReadyState <> 4: Loop
End With


Set HTMLDoc = oBrowser.Document

With HTMLDoc
  HTMLDoc.getElementById("username").Value = "user"
  HTMLDoc.getElementById("password").Value = "password"
End With

For Each oHTML_Element In HTMLDoc.getElementsByTagName("button")
  If oHTML_Element.Name = "Submit" Then oHTML_Element.Click
  Exit For
Next

oBrowser.Quit

End Sub

#1


1  

I just tested this code and it worked.

我刚刚测试了这段代码,它成功了。

The only difference is that I used Late Binding (too lazy to set all references) and I added While .Busy to the Loop to wait for the URL to load. I also changed input to button in the button click loop.

惟一的区别是,我使用了后期绑定(太懒了,无法设置所有引用),然后我添加了。我还在按钮单击循环中将输入改为按钮。

Dim HTMLDoc As Object 'HTMLDocument
Dim oBrowser As Object 'InternetExplorer

Sub Website_Login()

Dim oHTML_Element As Object 'IHTMLElement
Dim sURL As String

sURL = "https://www.mypage.com/login.html"
Set oBrowser = CreateObject("InternetExplorer.Application") 'New InternetExplorer
'oBrowser.Silent = True
'oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True

With oBrowser
    Do While .Busy Or .ReadyState <> 4: Loop
End With


Set HTMLDoc = oBrowser.Document

With HTMLDoc
  HTMLDoc.getElementById("username").Value = "user"
  HTMLDoc.getElementById("password").Value = "password"
End With

For Each oHTML_Element In HTMLDoc.getElementsByTagName("button")
  If oHTML_Element.Name = "Submit" Then oHTML_Element.Click
  Exit For
Next

oBrowser.Quit

End Sub