提问者:小点点

使用VBA从div类中拉URL窗体<一个href元素


团队

我的问题是,如何让VBA只抓取'href URL链接列表‘?

下面是我的代码:

Sub DW()

Dim Dr As New Selenium.EdgeDriver, Attach as Object, aList as Object

With Dr
    .Get "URL"
    .Wait 3000
End With

Set Attach = Dr.FindElementsByXPath("//[@id='OrderAttached'][1]") 
For Each Attach In Dr.FindElementsByXPath("//[@id='OrderAttached'][1]")
Set aList = Dr.FindElementsByCss("#OrderAttached [title]")
X = Dr.FindElementByXPath("//strong[@data-bind='text:$root.attachmentsViewModel.attachments().length'][1]").Text
With Sheet2
       Lr = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row + 1
       For i = 1 To X
            Sheet2.Cells(Lr, 1) = aList.item(i).Text
            Lr = Lr + 1
        Next
End With
Next
' do some stuff

End Sub

使用上面的代码,它像这样提取href的内部文本

Cell(2,1) = order_found.jpg
Cell(2,2) = order_not_found.jpg

我希望它返回值到我的电子表格,如下所示

Cell(2,1) = "https://URL/Documents/GetTempLink?attachmentId=201803%2f93b14b2a-1c10-40f6-a3f6-0e83eea67bf2-check_out_2018.03.09_22%3a16%3a53.jpg&amp;overridefilename=check_out_2018.03.09_22%3a16%3a53.jpg&amp;over rideFileName=check_out_2018.03.09_22%3a16%3a53.jpg"
Cell(2,2) = "https://URL/Documents/GetTempLink?attachmentId=201803%2fa21245ab-dd7f-4da0-bb31-d308f935527e-check_out_2018.03.09_22%3a15%3a47.jpg&amp;overridefilename=check_out_2018.03.09_22%3a15%3a47.jpg&amp;over  rideFileName=check_out_2018.03.09_22%3a15%3a47.jpg"

null

<div class="col-sm-3 col-xs-12 pull-right attachments-files-list" id="OrderAttached">
            <button class="btn btn-success primary wo-attachments-btn js-app-action-el" data-bind="visible: !$root.providerShouldAcceptDecline() &amp;&amp; !isOutsourced(), click : addAttachment"><i class="fa fa-plus" aria-hidden="true"></i>Add File</button>
            <div class="row wo-download-attachments-link">
                <a href="#" class="btn btn-default" data-bind="click : downloadAttachments, visible: $root.attachmentsViewModel.attachments().length > 0">
                    <i class="fa fa-download js-app-action-el" aria-hidden="true"></i>
                    Click  <strong data-bind="text:$root.attachmentsViewModel.attachments().length">2</strong> Links
                </a>
            </div>

            <div class="wo-attachments-link">
                <a href="#" class="js-app-action-el" data-bind="click: $root.attachmentsViewModel.showDeleteAttachmentDialog"><i class="fa fa-trash-o" aria-hidden="true"></i></a>
                <a href="https://URL/Documents/GetTempLink?attachmentId=201803%2f93b14b2a-1c10-40f6-a3f6-0e83eea67bf2-check_out_2018.03.09_22%3a16%3a53.jpg&amp;overridefilename=check_out_2018.03.09_22%3a16%3a53.jpg&amp;overrideFileName=check_out_2018.03.09_22%3a16%3a53.jpg" class="js-app-action-el" data-bind="text:fileName, attr: { href: url, title: description }, style: { 'padding-left': $parent.isOutsourced() ? 30 : 0 }" target="_blank" title="check_out_2018.03.09_22:16:53.jpg" style="padding-left: 0px;">order_found.jpg</a>
                <span class="wo-attach-date">
                    <span data-bind="text:uploadDate">02/09/2018  </span>
                </span>
            </div>
            
            <div class="wo-attachments-link">
                <a href="#" class="js-app-action-el" data-bind="click: $root.attachmentsViewModel.showDeleteAttachmentDialog"><i class="fa fa-trash-o" aria-hidden="true"></i></a>
                <a href="https://URL/Documents/GetTempLink?attachmentId=201803%2fa21245ab-dd7f-4da0-bb31-d308f935527e-check_out_2018.03.09_22%3a15%3a47.jpg&amp;overridefilename=check_out_2018.03.09_22%3a15%3a47.jpg&amp;overrideFileName=check_out_2018.03.09_22%3a15%3a47.jpg" class="js-app-action-el" data-bind="text:fileName, attr: { href: url, title: description }, style: { 'padding-left': $parent.isOutsourced() ? 30 : 0 }" target="_blank" title="check_out_2018.03.09_22:15:47.jpg" style="padding-left: 0px;">order_not_Found.jpg</a>
                <span class="wo-attach-date">
                    <span data-bind="text:uploadDate">02/09/2018  </span>
                </span>
            </div>
        </div>

null

如果需要其他信息,请告诉我。 谢谢你的帮助!


共1个答案

匿名用户

您可以直接使用xpath方法抓取属性,

Set aList = Dr.FindElementByXPath("//a[@class='js-app-action-el' and contains(@title,'jpg')]/@href")

这应该返回两个值,您可以根据需要对其进行处理,

Attribute='href=https://URL/Documents/GetTempLink?attachmentId=201803%2f93b14b2a-1c10-40f6-a3f6-0e83eea67bf2-check_out_2018.03.09_22%3a16%3a53.jpg&overridefilename=check_out_2018.03.09_22%3a16%3a53.jpg&overrideFileName=check_out_2018.03.09_22%3a16%3a53.jpg'
Attribute='href=https://URL/Documents/GetTempLink?attachmentId=201803%2fa21245ab-dd7f-4da0-bb31-d308f935527e-check_out_2018.03.09_22%3a15%3a47.jpg&overridefilename=check_out_2018.03.09_22%3a15%3a47.jpg&overrideFileName=check_out_2018.03.09_22%3a15%3a47.jpg'