VBA-IE10存储表对象? [英] VBA-IE10 Storing table Object?
问题描述
我已经找到了如何通过基于Web的表进行旋转以查找特定信息。在表的末尾是一些按钮,我想要与之后进行交互< td>
内部文本是Amend
。然而,这是在源代码中< td>< a onclick =返回确认(&#39;您确定要修改此预订吗?&#39;); id =MainContent_rptBookings_btnAmendBooking_0class =btn_amendhref =javascript:__ doPostBack(&#39; ctl00 $ MainContent $ rptBookings $ ctl01 $ btnAmendBooking&#39;,&#39;&#39;)>修改< / a>< / td>
所以这是我的循环代码
设置tbl = ie.document.getElementsByTagName(table)(1)
设置trs = tbl.getElementsByTagName(tr)
对于r = 0 To trs.Length - 1
设置tds = trs(r).getElementsByTagName(td)
'如果没有< td>然后寻找< th>
如果tds.Length = 0则设置tds = trs(r).getElementsByTagName(th)
对于c = 0到tds.Length - 1
如果tds( c).innerText喜欢471 *然后
存储(d,0)= tds(c).innerText
存储(d,1)= tds(c + 2).innerText
存储(d,2)= tds(c + 4).innerText
如果tds(c + 6).innerText =Amend那么
设置Amen_BTN(d,0)= tds (c + 6)。****但是这里有什么用的!?*****
结束如果
d = d + 1
Debug.Print tds(c) .innerText
结束如果
下一个c
下一个r
所以*****中的内容允许我存储按钮以便稍后在 Amen_BTN(0,0)上使用。点击
我希望这是一个可以理解的问题,
感谢您花时间阅读本文!
看着html-sni页面的ppet和阅读你的评论我终于明白你想要实现的目标。但这不是IMO :(。
最后一个单元格有以下内容:
< td>
< a onclick =返回确认(&#39;您确定要修改此预订吗?&#39;);
id =MainContent_rptBookings_btnAmendBooking_0
class =btn_amendhref =javascript:__ doPostBack(&#39; ctl00 $ MainContent $ rptBookings $ ctl01 $ btnAmendBooking&#39;,&#39;&#39; )>
修改
< / a>
< / td>
因此它包含带有java-script confirm调用的 anchor
元素。当用户确认修改时,<$ c将调用$ c> _doPostBack 函数接收导致
I have figured out how to rotate through a web based table to find specific information. At the end of the table are some buttons i will want to interact with later the <td>
inner text is "Amend"
. this however is in the source code as <td><a onclick="return confirm('Are you sure you want to Amend this booking?');" id="MainContent_rptBookings_btnAmendBooking_0" class="btn_amend" href="javascript:__doPostBack('ctl00$MainContent$rptBookings$ctl01$btnAmendBooking','')">Amend</a></td>
so here is my looping code
Set tbl = ie.document.getElementsByTagName("table")(1)
Set trs = tbl.getElementsByTagName("tr")
For r = 0 To trs.Length - 1
Set tds = trs(r).getElementsByTagName("td")
'if no <td> then look for <th>
If tds.Length = 0 Then Set tds = trs(r).getElementsByTagName("th")
For c = 0 To tds.Length - 1
If tds(c).innerText Like "471*" Then
Storage(d, 0) = tds(c).innerText
Storage(d, 1) = tds(c + 2).innerText
Storage(d, 2) = tds(c + 4).innerText
If tds(c + 6).innerText = "Amend" Then
Set Amen_BTN(d, 0) = tds(c + 6).****BUT WHAT GOES HERE!?*****
End If
d = d + 1
Debug.Print tds(c).innerText
End If
Next c
Next r
So what goes in the ***** to allow me store the button to use later on Amen_BTN(0, 0).Click
I hope this is an understandable question,
Thank you for taking the time to read this!
Looking at the html-snippet of the page and reading your comments I finally understand what are you trying to achieve. But this is IMO not possible :(.
The last cell has the following content:
<td>
<a onclick="return confirm('Are you sure you want to Amend this booking?');"
id="MainContent_rptBookings_btnAmendBooking_0"
class="btn_amend" href="javascript:__doPostBack('ctl00$MainContent$rptBookings$ctl01$btnAmendBooking','')">
Amend
</a>
</td>
So it contains an anchor
element with java-script confirm call. When the user confirms the amend then the _doPostBack
function will be called receiving the name of the control which caused the post-back which then performs the submit of the form
. So the whole page is send to the server in a POST request and on the server it does something with the data and returns a resulting page back. The it denotes some server logic which expects some parameters and returns results. So when the grid tblResults
doesn't offer bulk processing of more then one item at a time, then it is probably not possible or safe to process more then one at a time. So here you can just click one amend and let the server to process the request and then get the new data back from the server and process next one etc. Is it understandable? HTH
EDIT:
- How to click the
anchor
element with VBA:Here in this example
querySelector
method is used. We specify the selector to the specificanchor
element e.g. like this:div[id='divResults'] table[id='tblResults'] tbody td a[id='MainContent_rptBookings_btnAmendBooking_0']
The complete code then could look like this, HTH.
Option Explicit
' Add reference to Microsoft Internet Controls (SHDocVw)
' Add reference to Microsoft HTML Object Library
Sub ClickOnAmendBooking()
Dim ie As SHDocVw.InternetExplorer
Dim doc As MSHTML.HTMLDocument
Dim url As String
url = "file:///c:/Temp/StackOverflow/html/"
Set ie = New SHDocVw.InternetExplorer
ie.Visible = True
ie.navigate url & "Chris_livermore.html"
While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set doc = ie.document
Dim btnAmendBooking As HTMLAnchorElement
Set btnAmendBooking = doc.querySelector("div[id='divResults'] table[id='tblResults'] tbody td a[id='MainContent_rptBookings_btnAmendBooking_0']")
If Not btnAmendBooking Is Nothing Then
btnAmendBooking.Click
Else
MsgBox "btnAmendBooking was not found on webpage"
End If
ie.Quit
Set ie = Nothing
End Sub
Result:
这篇关于VBA-IE10存储表对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!