简体   繁体   English

使用带有 VBA excel 的 Internet Explorer 获取 HTML 链接(没有代码错误,但结果错误)

[英]Getting HTML link using Internet explorer with VBA excel (no code error but the results is wrong)

Automating an Internet explorer process by using VBA excel.使用 VBA excel 自动化 Internet Explorer 进程。 I am trying to get the first link in this classname 'datacell' with the th scope=row in html using the first 'a href' tag as my needed link.我正在尝试使用第一个 'a href' 标记作为我需要的链接,在 html 中使用第一个范围 = 行来获取这个类名“datacell”中的第一个链接。 however the result im getting is the wrong link.但是我得到的结果是错误的链接。 Could anyone help me about this?谁能帮我解决这个问题? There is no error in the code but only the results im getting代码中没有错误,但只有我得到的结果

refer to the image link - Correct link is the black circled one but the result is red circled one参考图片链接 - 正确的链接是黑色圆圈的,但结果是红色圆圈的

在此处输入图像描述

This is my VBA code:这是我的 VBA 代码:

         Set appIE = New InternetExplorerMedium
           With appIE
          .Visible = False
          .Visible = True
                 Set doc = appIE.document
                    For Each li In doc.getElementsByClassName("list")
                    
                    checkIfAttachment = li.innerHTML
                    
                    If InStr(checkIfAttachment, "No matches found") Then
                        ws.Cells(i, 6).Value = "Not in SFDC"
                    
                    Else
                    
                        Set childLi = Nothing
                        For Each childLi In doc.getElementsByClassName(" dataCell  ")
                            link = childLi.getElementsByTagName("a")(0)
                            'first link is the one we want
                            
                        Next childLi
                        
                        link = Replace(link, "https://sample.com", "")
                        link = Replace(link, "?srPos=0&srKp=00P", "")
                        .navigate baseSFDCDownloadLink & link
                        Do While .Busy = True Or .readyState <> 4: DoEvents: Loop
                        Application.Wait (Now + TimeValue("0:00:05"))

If you are getting a td rather than th element then specify th eg with type css selector (adding class as well)如果您获得的是td而不是th元素,则使用th 8CBA22E28EB17B5F5C6AE2A266AZ 选择器type指定例如(也添加 class)

.document.querySelector("th.dataCell").href

you don't need a loop for "datacell".您不需要“数据单元”的循环。 I'm not familiar with VBA but something like this could work.我对 VBA 不熟悉,但这样的东西可以工作。

link = doc.getElementsByClassName(" dataCell ")(0).getElementsByTagName("a")(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM