gpt4 book ai didi

excel - 如何使用 VBA 单击 HTML 链接

转载 作者:行者123 更新时间:2023-12-04 20:03:53 26 4
gpt4 key购买 nike

我正在尝试单击网站中的链接,如下面的屏幕截图所示。此链接的 HTML 代码类似于 <a target="_blank" href="gamit_main.htm?gamitId=163734">'3311-10310</a> .

Sample Screenshot

这样我就有一个代码可以单击链接,例如:

Set HTMLDoc = ie.Document
HTMLDoc.getElementsByTagName("a").Click

但我得到了错误:

"Object doesn't support this property or method".



引用代码快照:

Code Screenshot

完整代码:
Sub Something()
Dim ie As Object
Dim HTMLDoc As MSHTML.HTMLDocument
Dim ckt_No As String
ckt_No = Range("A2").Value
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ShowWindow ie.Hwnd, SW_MAXIMIZE
ie.Navigate "http://xyw.htm"
Do While ie.Busy = True Or ie.ReadyState <> 4: DoEvents: Loop
ie.Navigate "http:dgetcjdm_ckt_No&display_content=Y&noFormFields=Y&refresh=Y"
Do While ie.Busy = True Or ie.ReadyState <> 4: DoEvents: Loop
Set HTMLDoc = ie.Document
HTMLDoc.getElementById("resultRow").getElementByTagName("a")(0).Click
End Sub

HTML 代码如下:
        <td align="center" height="15" title="1" class="tdborder">

<font class="rtabletext">1</font>

</td>



<td align="Left" title="GAMITARM Status Date" class="tdborder" nowrap="">

<font class="rtabletext">19-MAR-2020</font>

</td>

<td align="Left" title="Circuit Number" class="tdborder" nowrap="">

<font class="rtabletext"><a target="_blank" href="gamit_main.htm?gamitId=168592">'70F934C8</a></font>

</td>

<td align="Left" title="CUSTOMER" class="tdborder" nowrap="">

<font class="rtabletext">MICROSOFT CORPORATION</font>

</td>

<td align="Left" title="Customer Id" class="tdborder" nowrap="">

<font class="rtabletext">8684</font>

</td>


<td align="Left" title="AO AM" class="tdborder" nowrap="">

<font class="rtabletext">&nbsp;</font>

</td>

<td align="Left" title="AO SD" class="tdborder" nowrap="">

<font class="rtabletext">&nbsp;</font>

</td>

<td align="Left" title="AO ED/AVP" class="tdborder" nowrap="">

<font class="rtabletext">&nbsp;</font>

</td>

<td align="Left" title="AO VP" class="tdborder" nowrap="">

<font class="rtabletext">&nbsp;</font>

<td align="Left" title="LCON Phone Contact (SM Feed) " class="tdborder" nowrap="">

<font class="rtabletext">&nbsp;</font>

</td>

<td align="Left" title="LCON Cell Contact (SM Feed) " class="tdborder" nowrap="">

<font class="rtabletext">&nbsp;</font>


</td>

<td align="Left" title="Programme Office Status" class="tdborder" nowrap="">

<font class="rtabletext">New</font>


</td>

<td align="Left" title="Major Initiative" class="tdborder" nowrap="">

<font class="rtabletext">Ethernet</font>

</td>

<td align="Left" title="Project" class="tdborder" nowrap="">

<font class="rtabletext">APAC Singapore Ethernet Tail Rolls</font>

</td>

<td align="Left" title="Phase" class="tdborder" nowrap="">

<font class="rtabletext">x.2.2.a</font>


</td>

<td align="Left" title="LOA received" class="tdborder" nowrap="">

<font class="rtabletext">NO</font>

</td>

<td align="Left" title="Technical Connectivity details received" class="tdborder" nowrap="">

<font class="rtabletext">NO</font>

</td>

</tr>

并试图专注于这条线
            <font class="rtabletext"><a target="_blank" href="gamit_main.htm?gamitId=168592">'70F934C8</a></font>

这里有一些 parent 的快照:请找到突出显示的元素

Iframe_Highlight
Iframe_TagName -a_Highlight

最佳答案

我会将父节点的类选择器与属性 = 值选择器(使用包含运算符 * )结合起来,通过它的 href 和它的值来定位子标签

htmlDoc.querySelector(".rtabletext [href*='gamitId=']").click

无需检索多个节点或使用循环

这确实假设匹配的第一个节点是所需的节点。您始终可以将 '' 之间使用的 href 值扩展为更多选择器(或者更一般地扩展 querySelector 使用的 css 选择器以定位精确节点)。

css selectors

关于excel - 如何使用 VBA 单击 HTML 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61142348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com