网站设置输入框值失败
Failing to Set Input Box Values on Website
我有以下 VBA 代码:
Sub OpenWebPage()
Dim oIE As Object
Dim sURL As String
Dim HTML As HTMLDocument, hDataManager As IHTMLElementCollection, hDropSelect
Dim HWNDSrc As Long
Dim itm As Variant
'objects
Set oIE = CreateObject("InternetExplorer.Application")
'//---Open the browser and log in, then navigate to the data manager
oIE.silent = True 'No pop-ups
oIE.Visible = True
sURL = "https://publicisuk.lumina.mediaocean.com/Admin/DataManager.aspx"
oIE.navigate sURL
'wait for process to complete before executing the next task
Do While oIE.Busy: DoEvents: Loop
Do Until oIE.ReadyState = 4: DoEvents: Loop
'get window ID for IE so we can set it as active window
HWNDSrc = oIE.HWND
'set IE as active window
SetForegroundWindow HWNDSrc
'loop through elements/ items to find username field
For Each itm In oIE.document.All
If itm = "[object HTMLInputElement]" Then
If itm.Name = "username" Then
Debug.Print "Username field found!"
itm.Value = "johnsmith@gmail.com"
Debug.Print "Username: " & itm.Value
Exit For
End If
End If
Next itm
'now loop through to find password element
For Each itm In oIE.document.All
If itm = "[object HTMLInputElement]" Then
If itm.Name = "password" Then
Debug.Print "Password field found!"
itm.Value = "IAmAMonkey"
Debug.Print "Password: " & itm.Value
Application.SendKeys "~", True
Exit For
End If
End If
Next itm
它所做的是将 Internet Explorer 加载到一个对象,然后导航到我要登录的 URL。我遍历网页上的元素以检查 InputElements
.当找到 Username
输入框时,它会将 .Value
设置为用户名,当找到 Password
输入框时,它会将 .Value
设置为密码,之后我发送 Enter
密钥,大概是为了登录。
现在,有趣的问题。当我 运行 它打印出它找到了相应的字段,并且还打印出它们的新设置值,但是,框中根本没有文本出现。
我也尝试过以下语法:
oIE.Document.getElementById("username").Value = "johnsmith@gmail.com"
oIE.Document.getElementsByName("username").Value = "johnsmith@gmail.com"
结果相同 - 没有错误,但值未显示在框中。
有谁知道为什么会这样或者我的方法有什么问题吗?
下面是 HTML 代码。
<div id="login-panel">
<input name="username" class="form-required-field" id="username" accesskey="u" type="text" placeholder="Username" value="" data-bind="value: form().username, placeholder: bundle['user.label.html']" htmlescape="true" autocomplete="off" cssclass="form-required-field"><!-- organization select goes here if multiple organizations use the same domain --><div class="inline-organization-panel" id="inline-organization-panel" style="display: none;" data-bind="moSlideVisible: isOrganisationSelectPanelVisible()">
<span id="inline-organization-label" data-bind="text: bundle['organization.panel.label']">Choose organization.</span>
<div data-bind="foreach: organisationInfoArray"></div>
</div>
<input name="password" class="form-required-field" id="password" accesskey="p" type="password" placeholder="Password" value="" data-bind="value: form().password, placeholder: bundle['password.label.html']" htmlescape="true" autocomplete="off" cssclass="form-required-field" csserrorclass="error"><!-- Continent selection goes here if mf-style username and don't have continent cookie -->
这是我到达的地方,稍后我会再试一次,但在此期间可能会有所帮助。
Dim ie As InternetExplorer
Dim d As HTMLDocument
Dim f As HTMLFormElement
Set ie = New InternetExplorer
ie.Visible = 1
ie.navigate "https://publicisuk.lumina.mediaocean.com/Admin/DataManager.aspx"
Do While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set d = ie.document
Set f = d.forms(0)
'Putting a break point here and waiting a moment allows access
f.elements("username").Value = "username"
我测试发现代码会 运行 在设置值之前设置断点但不会 运行 没有断点。
我认为可能是调试模式增加了更多的延迟,所以我尝试在代码中添加一个单独的延迟并且它起作用了。您可以在下面查看我的示例:
Sub LOADIE()
Set ieA = CreateObject("InternetExplorer.Application")
ieA.Visible = True
ieA.navigate "https://publicisuk.lumina.mediaocean.com/mo-cas/login"
Do Until ieA.readyState = 4
DoEvents
Loop
delay 4
Set doc = ieA.Document
Set UserName = doc.getElementById("username")
UserName.Value = "johnsmith@gmail.com"
Set Password = doc.getElementById("password")
Password.Value = "IAmAMonkey"
Set btn = doc.getElementById("buttonSignin")
btn.Disabled = False
btn.Click
End Sub
Private Sub delay(seconds As Long)
Dim endTime As Date
endTime = DateAdd("s", seconds, Now())
Do While Now() < endTime
DoEvents
Loop
End Sub
我会在分配值之前关注每个元素,然后您只需从提交按钮中删除禁用属性
Option Explicit
Public Sub LogIn()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.Navigate2 "https://publicisuk.lumina.mediaocean.com/mo-cas/login"
While .Busy Or .readyState <> 4: DoEvents: Wend
With .document
With .querySelector("#username")
.Focus
.Value = "johnsmith@gmail.com"
End With
With .querySelector("#password")
.Focus
.Value = "IAMaMonkey"
End With
With .querySelector("#buttonSignin")
.removeAttribute "disabled"
.Click
End With
End With
Stop
End With
End Sub
我有以下 VBA 代码:
Sub OpenWebPage()
Dim oIE As Object
Dim sURL As String
Dim HTML As HTMLDocument, hDataManager As IHTMLElementCollection, hDropSelect
Dim HWNDSrc As Long
Dim itm As Variant
'objects
Set oIE = CreateObject("InternetExplorer.Application")
'//---Open the browser and log in, then navigate to the data manager
oIE.silent = True 'No pop-ups
oIE.Visible = True
sURL = "https://publicisuk.lumina.mediaocean.com/Admin/DataManager.aspx"
oIE.navigate sURL
'wait for process to complete before executing the next task
Do While oIE.Busy: DoEvents: Loop
Do Until oIE.ReadyState = 4: DoEvents: Loop
'get window ID for IE so we can set it as active window
HWNDSrc = oIE.HWND
'set IE as active window
SetForegroundWindow HWNDSrc
'loop through elements/ items to find username field
For Each itm In oIE.document.All
If itm = "[object HTMLInputElement]" Then
If itm.Name = "username" Then
Debug.Print "Username field found!"
itm.Value = "johnsmith@gmail.com"
Debug.Print "Username: " & itm.Value
Exit For
End If
End If
Next itm
'now loop through to find password element
For Each itm In oIE.document.All
If itm = "[object HTMLInputElement]" Then
If itm.Name = "password" Then
Debug.Print "Password field found!"
itm.Value = "IAmAMonkey"
Debug.Print "Password: " & itm.Value
Application.SendKeys "~", True
Exit For
End If
End If
Next itm
它所做的是将 Internet Explorer 加载到一个对象,然后导航到我要登录的 URL。我遍历网页上的元素以检查 InputElements
.当找到 Username
输入框时,它会将 .Value
设置为用户名,当找到 Password
输入框时,它会将 .Value
设置为密码,之后我发送 Enter
密钥,大概是为了登录。
现在,有趣的问题。当我 运行 它打印出它找到了相应的字段,并且还打印出它们的新设置值,但是,框中根本没有文本出现。
我也尝试过以下语法:
oIE.Document.getElementById("username").Value = "johnsmith@gmail.com"
oIE.Document.getElementsByName("username").Value = "johnsmith@gmail.com"
结果相同 - 没有错误,但值未显示在框中。
有谁知道为什么会这样或者我的方法有什么问题吗?
下面是 HTML 代码。
<div id="login-panel">
<input name="username" class="form-required-field" id="username" accesskey="u" type="text" placeholder="Username" value="" data-bind="value: form().username, placeholder: bundle['user.label.html']" htmlescape="true" autocomplete="off" cssclass="form-required-field"><!-- organization select goes here if multiple organizations use the same domain --><div class="inline-organization-panel" id="inline-organization-panel" style="display: none;" data-bind="moSlideVisible: isOrganisationSelectPanelVisible()">
<span id="inline-organization-label" data-bind="text: bundle['organization.panel.label']">Choose organization.</span>
<div data-bind="foreach: organisationInfoArray"></div>
</div>
<input name="password" class="form-required-field" id="password" accesskey="p" type="password" placeholder="Password" value="" data-bind="value: form().password, placeholder: bundle['password.label.html']" htmlescape="true" autocomplete="off" cssclass="form-required-field" csserrorclass="error"><!-- Continent selection goes here if mf-style username and don't have continent cookie -->
这是我到达的地方,稍后我会再试一次,但在此期间可能会有所帮助。
Dim ie As InternetExplorer
Dim d As HTMLDocument
Dim f As HTMLFormElement
Set ie = New InternetExplorer
ie.Visible = 1
ie.navigate "https://publicisuk.lumina.mediaocean.com/Admin/DataManager.aspx"
Do While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set d = ie.document
Set f = d.forms(0)
'Putting a break point here and waiting a moment allows access
f.elements("username").Value = "username"
我测试发现代码会 运行 在设置值之前设置断点但不会 运行 没有断点。
我认为可能是调试模式增加了更多的延迟,所以我尝试在代码中添加一个单独的延迟并且它起作用了。您可以在下面查看我的示例:
Sub LOADIE()
Set ieA = CreateObject("InternetExplorer.Application")
ieA.Visible = True
ieA.navigate "https://publicisuk.lumina.mediaocean.com/mo-cas/login"
Do Until ieA.readyState = 4
DoEvents
Loop
delay 4
Set doc = ieA.Document
Set UserName = doc.getElementById("username")
UserName.Value = "johnsmith@gmail.com"
Set Password = doc.getElementById("password")
Password.Value = "IAmAMonkey"
Set btn = doc.getElementById("buttonSignin")
btn.Disabled = False
btn.Click
End Sub
Private Sub delay(seconds As Long)
Dim endTime As Date
endTime = DateAdd("s", seconds, Now())
Do While Now() < endTime
DoEvents
Loop
End Sub
我会在分配值之前关注每个元素,然后您只需从提交按钮中删除禁用属性
Option Explicit
Public Sub LogIn()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.Navigate2 "https://publicisuk.lumina.mediaocean.com/mo-cas/login"
While .Busy Or .readyState <> 4: DoEvents: Wend
With .document
With .querySelector("#username")
.Focus
.Value = "johnsmith@gmail.com"
End With
With .querySelector("#password")
.Focus
.Value = "IAMaMonkey"
End With
With .querySelector("#buttonSignin")
.removeAttribute "disabled"
.Click
End With
End With
Stop
End With
End Sub