我试图在VBA中编写一段代码,允许我打开一个网站并登录。我遇到的问题是用户名和密码将填入正确的框,但在按下按钮之前消失。我已经尝试在代码的“按钮”部分之前添加一个计时器,但这只会延迟不可避免的!到目前为止,这是我的代码:
'Declare constants
Const cURL = "website url"
Const cusername = "my username"
Const cpass = "my password"
'Declare Variables
Dim IE As InternetExplorer
Dim iedoc As Object
Dim objCollection As Object
'Open internet explorer and navigate to website
Set IE = New InternetExplorer
IE.Visible = True
IE.Navigate cURL
'Wait for initial page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
'Populate the username and password fields
Set iedoc = IE.document
With iedoc.forms(0)
.UserId.Value = cusername
.Password.Value = cpass
Application.Wait (Now + TimeValue("0:00:10"))
Set ojbCollection = IE.document.getElementsByClassName("HippoButton__default__758acd9f5b27a3904664f41ba069d55a LoginComponents__loginButton__214681ee5e89039a688aeca993b3b95c")(0).Click
End With
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
以下是网站上的HTML源代码:
<form>
<div class="LoginComponents__fullField__9ce1cf562b2ae67284a157be3b4f1c8d">
<label>User Id</label>
<input type="text" name="userId" value=""
class="HippoInput__text__3a7f7c41dab2f6282c3d2416f11729c7
HippoInput__invalid__18e2ce0e6d6a0c36106c94a2a7d06e29">
</div>
<div class="LoginComponents__fullField__9ce1cf562b2ae67284a157be3b4f1c8d">
<label>Password</label>
<input type="password" name="password" value=""
class="HippoInput__text__3a7f7c41dab2f6282c3d2416f11729c7
HippoInput__invalid__18e2ce0e6d6a0c36106c94a2a7d06e29">
</div>
<div
class="LoginComponents__checkboxField__021cae0465786c49cfbe6782549172f2">
<input type="checkbox" name="rememberMe"
class="HippoInput__checkbox__83aa5a268198870efcbb1bd13434d2af" value="on">
<label>Remember Me</label>
</div>
<div class="LoginComponents__fullField__9ce1cf562b2ae67284a157be3b4f1c8d">
<button class="HippoButton__default__758acd9f5b27a3904664f41ba069d55a
LoginComponents__loginButton__214681ee5e89039a688aeca993b3b95c">Secure
Login</button>
</div>
</form>