Element.Click 打开第一项但不打开第二项
Element.Click opens first item but not second
尝试通过单击 MSHTML.IHTMLElement TagName 打开 tables,如果它匹配 tag.id。
tag.id 立即显示在 window 中作为我正在搜索的那个,它会打开 table.
第二个 tag.id 是相等的,但 table 没有打开,但程序运行完成。
我发现其他问题说我应该摆脱 For Each 语句并使用 For to 语句(我在下面进行了编辑)但我得到了相同的结果,打开第一个而不是第二个。第二次循环 elementTagName.ID 被设置为空。
该网站使用 AJAX 将所有数据加载到页面,并且加载时间过长,包括 element.click 后面的 table 所以建议关于如何用更清洁的东西处理睡眠呼叫可能会打开第二个 table。
我做了广泛的研究来回答这个问题,但没有找到任何我可以使用的代码。
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Option Explicit
Public Sub AOGCQueryHTMLDocument2()'''
Dim ie As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.IHTMLDocument
Dim HTMLLoc As MSHTML.IHTMLElement, HTMLstr1 As MSHTML.IHTMLElement, HTMLWlBtn As MSHTML.IHTMLElement
Dim HTMLHide As MSHTML.IHTMLElement, HTMLstrWait As MSHTML.IHTMLElement
Dim HTMLHSearch As MSHTML.IHTMLElement
Dim HTMLWlBtns As MSHTML.IHTMLElementCollection
Dim btnShow As Long, IntWlCtr As Integer
Dim WlBtnCompare As String, i As Integer
ie.Visible = True
ie.navigate "http://www.aogc.state.ar.us/data/querybuild.aspx"
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = ie.document
'Sets and Clicks to open the location from which wells will be searched
Set HTMLLoc = HTMLDoc.getElementById("cpMainContent_ChildContent2_btnViewLocation")
HTMLLoc.Click
Sleep 7500
'Clicks on the Location
Set HTMLstr1 = HTMLDoc.getElementById("cpMainContent_ChildContent2_GridLocation_ckLocation_1")
With HTMLstr1
.Click
End With
'Hides the Location dropdown so can be searched
Set HTMLHide = HTMLDoc.getElementById("cpMainContent_ChildContent2_btnHideLocation")
HTMLHide.Click
Sleep 7500
'Searches for the Wells in the above Location
Set HTMLHSearch = HTMLDoc.getElementById("cpMainContent_ChildContent2_btnSearch")
HTMLHSearch.Click
Sleep 7500
Set HTMLDoc = ie.document
'Gets all "input" TagNames
Set HTMLWlBtns = HTMLDoc.getElementsByTagName("input")
IntWlCtr = 0
'Comparison of "input" tags to determine button to click
For i = 4 To HTMLWlBtns.Length - 1
With HTMLWlBtns
On Error Resume Next
Debug.Print HTMLWlBtns.Length, HTMLWlBtns.Item(4).ID, HTMLWlBtns.Item(5).ID
'Debug.Print shows length and nodes correctly
'HTMLWlBtns.Length = 20
'HTMLWlBtns.Item(4).ID = cpMainContent_ChildContent2_Repeater1_btnShow_0
'HTMLWlBtns.Item(5).ID = cpMainContent_ChildContent2_Repeater1_btnShow_1
End With
'Comparison of "input" tags to determine button to click
With HTMLWlBtn
Set HTMLWlBtn = HTMLWlBtns.Item(i)
End With
WlBtnCompare = "cpMainContent_ChildContent2_Repeater1_btnShow_" & IntWlCtr
'This Debug.Print the 1st time through the loop opens "btnShow_0"
'The 2nd time through the HTMLWlBtn.ID is nothing
'Need explanation as to why HTMLWlBtns.Item(5).ID gets emptied
If HTMLWlBtn.ID = WlBtnCompare Then
HTMLWlBtn.Click
Sleep 6500
IntWlCtr = IntWlCtr + 1
Set HTMLDoc = ie.document ' define it again
Set HTMLWlBtns = HTMLDoc.getElementsByTagName("input") 'I added
'this prior to Worf adding the previous Set trying to make sure HTMLWlBtns
'didn't get set to "nothing". Didn't work until Worf added ' define it again.
End If
Next i
MsgBox "Well opening is complete", vbSystemModal
End Sub
这是我要打开的 HTML 标签:
<div style="margin-left:15px; margin-right:5px; margin-bottom:5px;">
<table cellpadding="0" cellspacing="0">
<tbody><tr>
<td>
<input type="image" name="ctl00$ctl00$cpMainContent$ChildContent2$Repeater1$ctl00$btnShow" id="cpMainContent_ChildContent2_Repeater1_btnShow_0" src="../Images/plus.gif">
<span style="color:Black; font-weight:bold ">API</span> -
<span id="cpMainContent_ChildContent2_Repeater1_lblAPIWellNo_0" style="font-weight:bold;">03-077-00004-00-00</span>
<span style="color:Black; font-weight:bold ">Permit Number</span> -
<span id="cpMainContent_ChildContent2_Repeater1_lblPermit_0" style="font-weight:bold;">18684</span>
<span style="color:Black; font-weight:bold ">Well Name</span> -
<span id="cpMainContent_ChildContent2_Repeater1_lblWellName_0" style="font-weight:bold;">Bosnick Oper 1</span>
</td>
<td align="right">
|
<span style="color:Maroon; font-weight:bold ">SideTrack Cnt</span> -
<span id="cpMainContent_ChildContent2_Repeater1_lblRowTotal_0" style="font-size:10pt;font-weight:bold;">1</span>
</td>
</tr>
</tbody></table>
<table cellpadding="0" cellspacing="0">
<tbody><tr>
<td>
<input type="image" name="ctl00$ctl00$cpMainContent$ChildContent2$Repeater1$ctl01$btnShow" id="cpMainContent_ChildContent2_Repeater1_btnShow_1" src="../Images/plus.gif">
<span style="color:Black; font-weight:bold ">API</span> -
<span id="cpMainContent_ChildContent2_Repeater1_lblAPIWellNo_1" style="font-weight:bold;">03-077-10002-00-00</span>
<span style="color:Black; font-weight:bold ">Permit Number</span> -
<span id="cpMainContent_ChildContent2_Repeater1_lblPermit_1" style="font-weight:bold;">38665</span>
<span style="color:Black; font-weight:bold ">Well Name</span> -
<span id="cpMainContent_ChildContent2_Repeater1_lblWellName_1" style="font-weight:bold;">Bosnick 1-1</span>
</td>
<td align="right">
|
<span style="color:Maroon; font-weight:bold ">SideTrack Cnt</span> -
<span id="cpMainContent_ChildContent2_Repeater1_lblRowTotal_1" style="font-size:10pt;font-weight:bold;">1</span>
</td>
</tr>
</tbody></table>
<br><br>
</div>
终于从 MrExcel 版块上一位名叫 Worf 的“知名会员”那里得到了答案。
这是一个简单的答案,因为他在第一个元素被选中后在 if 语句中放置了另一个 Set HTMLDoc = ie.document
,然后是 Set HTMLWlBtns = HTMLDoc.getElementsByTagName("input")
。
不知道为什么它会丢失,但希望他能在论坛上解释发生了什么。
我已经更新了上面的代码以显示正确的答案。
这是 link 如果其他人遇到类似问题的问题:
https://www.mrexcel.com/board/threads/automation-macro-using-getelementsbytagname-opens-first-element-but-not-2nd.1156811/#post-5612948
尝试通过单击 MSHTML.IHTMLElement TagName 打开 tables,如果它匹配 tag.id。
tag.id 立即显示在 window 中作为我正在搜索的那个,它会打开 table.
第二个 tag.id 是相等的,但 table 没有打开,但程序运行完成。
我发现其他问题说我应该摆脱 For Each 语句并使用 For to 语句(我在下面进行了编辑)但我得到了相同的结果,打开第一个而不是第二个。第二次循环 elementTagName.ID 被设置为空。
该网站使用 AJAX 将所有数据加载到页面,并且加载时间过长,包括 element.click 后面的 table 所以建议关于如何用更清洁的东西处理睡眠呼叫可能会打开第二个 table。
我做了广泛的研究来回答这个问题,但没有找到任何我可以使用的代码。
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Option Explicit
Public Sub AOGCQueryHTMLDocument2()'''
Dim ie As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.IHTMLDocument
Dim HTMLLoc As MSHTML.IHTMLElement, HTMLstr1 As MSHTML.IHTMLElement, HTMLWlBtn As MSHTML.IHTMLElement
Dim HTMLHide As MSHTML.IHTMLElement, HTMLstrWait As MSHTML.IHTMLElement
Dim HTMLHSearch As MSHTML.IHTMLElement
Dim HTMLWlBtns As MSHTML.IHTMLElementCollection
Dim btnShow As Long, IntWlCtr As Integer
Dim WlBtnCompare As String, i As Integer
ie.Visible = True
ie.navigate "http://www.aogc.state.ar.us/data/querybuild.aspx"
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = ie.document
'Sets and Clicks to open the location from which wells will be searched
Set HTMLLoc = HTMLDoc.getElementById("cpMainContent_ChildContent2_btnViewLocation")
HTMLLoc.Click
Sleep 7500
'Clicks on the Location
Set HTMLstr1 = HTMLDoc.getElementById("cpMainContent_ChildContent2_GridLocation_ckLocation_1")
With HTMLstr1
.Click
End With
'Hides the Location dropdown so can be searched
Set HTMLHide = HTMLDoc.getElementById("cpMainContent_ChildContent2_btnHideLocation")
HTMLHide.Click
Sleep 7500
'Searches for the Wells in the above Location
Set HTMLHSearch = HTMLDoc.getElementById("cpMainContent_ChildContent2_btnSearch")
HTMLHSearch.Click
Sleep 7500
Set HTMLDoc = ie.document
'Gets all "input" TagNames
Set HTMLWlBtns = HTMLDoc.getElementsByTagName("input")
IntWlCtr = 0
'Comparison of "input" tags to determine button to click
For i = 4 To HTMLWlBtns.Length - 1
With HTMLWlBtns
On Error Resume Next
Debug.Print HTMLWlBtns.Length, HTMLWlBtns.Item(4).ID, HTMLWlBtns.Item(5).ID
'Debug.Print shows length and nodes correctly
'HTMLWlBtns.Length = 20
'HTMLWlBtns.Item(4).ID = cpMainContent_ChildContent2_Repeater1_btnShow_0
'HTMLWlBtns.Item(5).ID = cpMainContent_ChildContent2_Repeater1_btnShow_1
End With
'Comparison of "input" tags to determine button to click
With HTMLWlBtn
Set HTMLWlBtn = HTMLWlBtns.Item(i)
End With
WlBtnCompare = "cpMainContent_ChildContent2_Repeater1_btnShow_" & IntWlCtr
'This Debug.Print the 1st time through the loop opens "btnShow_0"
'The 2nd time through the HTMLWlBtn.ID is nothing
'Need explanation as to why HTMLWlBtns.Item(5).ID gets emptied
If HTMLWlBtn.ID = WlBtnCompare Then
HTMLWlBtn.Click
Sleep 6500
IntWlCtr = IntWlCtr + 1
Set HTMLDoc = ie.document ' define it again
Set HTMLWlBtns = HTMLDoc.getElementsByTagName("input") 'I added
'this prior to Worf adding the previous Set trying to make sure HTMLWlBtns
'didn't get set to "nothing". Didn't work until Worf added ' define it again.
End If
Next i
MsgBox "Well opening is complete", vbSystemModal
End Sub
这是我要打开的 HTML 标签:
<div style="margin-left:15px; margin-right:5px; margin-bottom:5px;">
<table cellpadding="0" cellspacing="0">
<tbody><tr>
<td>
<input type="image" name="ctl00$ctl00$cpMainContent$ChildContent2$Repeater1$ctl00$btnShow" id="cpMainContent_ChildContent2_Repeater1_btnShow_0" src="../Images/plus.gif">
<span style="color:Black; font-weight:bold ">API</span> -
<span id="cpMainContent_ChildContent2_Repeater1_lblAPIWellNo_0" style="font-weight:bold;">03-077-00004-00-00</span>
<span style="color:Black; font-weight:bold ">Permit Number</span> -
<span id="cpMainContent_ChildContent2_Repeater1_lblPermit_0" style="font-weight:bold;">18684</span>
<span style="color:Black; font-weight:bold ">Well Name</span> -
<span id="cpMainContent_ChildContent2_Repeater1_lblWellName_0" style="font-weight:bold;">Bosnick Oper 1</span>
</td>
<td align="right">
|
<span style="color:Maroon; font-weight:bold ">SideTrack Cnt</span> -
<span id="cpMainContent_ChildContent2_Repeater1_lblRowTotal_0" style="font-size:10pt;font-weight:bold;">1</span>
</td>
</tr>
</tbody></table>
<table cellpadding="0" cellspacing="0">
<tbody><tr>
<td>
<input type="image" name="ctl00$ctl00$cpMainContent$ChildContent2$Repeater1$ctl01$btnShow" id="cpMainContent_ChildContent2_Repeater1_btnShow_1" src="../Images/plus.gif">
<span style="color:Black; font-weight:bold ">API</span> -
<span id="cpMainContent_ChildContent2_Repeater1_lblAPIWellNo_1" style="font-weight:bold;">03-077-10002-00-00</span>
<span style="color:Black; font-weight:bold ">Permit Number</span> -
<span id="cpMainContent_ChildContent2_Repeater1_lblPermit_1" style="font-weight:bold;">38665</span>
<span style="color:Black; font-weight:bold ">Well Name</span> -
<span id="cpMainContent_ChildContent2_Repeater1_lblWellName_1" style="font-weight:bold;">Bosnick 1-1</span>
</td>
<td align="right">
|
<span style="color:Maroon; font-weight:bold ">SideTrack Cnt</span> -
<span id="cpMainContent_ChildContent2_Repeater1_lblRowTotal_1" style="font-size:10pt;font-weight:bold;">1</span>
</td>
</tr>
</tbody></table>
<br><br>
</div>
终于从 MrExcel 版块上一位名叫 Worf 的“知名会员”那里得到了答案。
这是一个简单的答案,因为他在第一个元素被选中后在 if 语句中放置了另一个 Set HTMLDoc = ie.document
,然后是 Set HTMLWlBtns = HTMLDoc.getElementsByTagName("input")
。
不知道为什么它会丢失,但希望他能在论坛上解释发生了什么。
我已经更新了上面的代码以显示正确的答案。
这是 link 如果其他人遇到类似问题的问题:
https://www.mrexcel.com/board/threads/automation-macro-using-getelementsbytagname-opens-first-element-but-not-2nd.1156811/#post-5612948