我正在进行一些自动化,其中数据将是excel并且需要使用excel值更新网站中的字段。我在网上找到了这个宏并尝试编辑它,但它不起作用。请注意,一旦登录成功,我可以继续更新字段。
我添加了以下参考:
问题:尝试在IE8中运行它。我需要更新到IE 11吗?收到错误:
对象'IWebBrowser2'的方法'Document'在线失败
更改:我已更新到IE11,现在错误已更改为
“自动化错误,未指定的错误”
IE.document.getelementsbyname("_58_login").Value = "jigarjigar"
Public Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long
Sub Automate_IE_Enter_Data()
'This will load a webpage in IE
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim HWNDSrc As Long
Dim document As HTMLDocument
'Create InternetExplorer Object
Set IE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
'Set IE = CreateObject("InternetExplorer.Application")
'True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Navigate to URL
IE.Navigate https://www.asite.com/login-home/
' Wait while IE loading...
Do While IE.ReadyState = 4: DoEvents: Loop
'Get Window ID for IE so we can set it as activate window
HWNDSrc = IE.HWND
'Set IE as Active Window
SetForegroundWindow HWNDSrc
'Find & Fill Out Input Box
IE.document.getelementsbyname("_58_login").Value = "jigarjigar"
IE.document.getelementsbyname("_58_password").Value = "mypassword"
IE.document.getelementsbyclassname("btn-submit nobgcolor").Click
'Unload IE
endmacro:
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End Sub
<table class="lfr-table">
<tr>
<td class="label-unm">
Login (Email)
</td>
<td>
<div class="inp-login"><input name="_58_login" type="text" value="[email protected]" onblur="checkforGSUser(this)" autocomplete="off"/></div>
</td>
</tr>
<tr>
<td class="label-pass">
Password
</td>
<td>
<div class="inp-login"><input id="_58_password" name="_58_password" type="password" autocomplete="off" value="" /></div>
<span id="_58_passwordCapsLockSpan" class="pwdCapsMsgSpan" style="position:absolute;display:none;"><table width="111" border="0" cellspacing="0" cellpadding="0"><tr><td class="pwdCapsBorder"><img src="/html/themes/asite/images/common/caps_msg_arrow.gif" hspace="10" /></td></tr><tr><td class="pwdCapsMsg"> Caps Lock is on.</td></tr></table></span>
</td>
</tr>
</table>
<div class="div-submit"> </div>
<div class="div-login-link">
<input class="btn-submit nobgcolor" type="image" src="/html/themes/asite/images/common/login.gif" />
<a target="_self" href="https://portal.asite.com/widget/web/guest/home?p_p_id=58&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view&_58_struts_action=%2Flogin%2Fview&_58_cmd=forgot-password">Forgot Password?</a>
<br/>
<a href="https://www.asite.com/contactus" target="_top">Don't have an account?</a>
<br/>
<div class="clear-all"></div>
<div class="clear-all"></div>
</div>
</form>
<script type="text/javascript">
这个:
Do While IE.ReadyState = 4: DoEvents: Loop
应该是这样的:
Do While IE.ReadyState <> 4 Or IE.Busy: DoEvents: Loop
在IE.ReadyState
枚举中,4 = READYSTATE_COMPLETE
,因为它代表你只是告诉代码只在页面完成服务时循环。
在此基础上,代码将不会在页面加载时等待(ReadyState 1 To 3),并且当您编辑它时,DOM中的元素可能尚未可用。
进一步说明:
HWND
改为LongPtr
。 (假设您使用的是x64版本 - 如果要在其他计算机上使用,则需要使用条件编译)。SetForegroundWindow
方法返回一个Long
,应该检查以查看消息是否在继续之前成功发送。IE.Document.getElementById("_58_login").Value
'// Class names don't have spaces in them, so the below is also wrong
IE.Document.getElementsByClassName("btn-submit nobgcolor")(0).Click
'// Or
IE.Document.Forms(0).Submit
getElements
IE.Quit
之前,请使用Nothing
关闭应用程序endmacro:
标签,因为您不会在任何时候更改错误处理或指导代码。在OP中看到HTML后:
For Each el In IE.Document.GetElementsByTagName("input")
If el.Name = "_58_login" Then
el.Value = "[email protected]"
Exit For
End If
Next
IE.Document.GetElementById("_58_password").Value = "Password"
IE.Document.GetElementsByClassName("btn-submit nobgcolor")(0).Click
'// or IE.Document.Forms(0).Submit