无法对某个链接执行单击操作

问题描述 投票:2回答:1

我在vba中使用IE编写了一些代码来访问某个网页。要获得该网页,必须在开始页面中启动单击链接,并且当成功执行单击时,我显示的页面将显示。但是,我找不到任何方法来点击这个链接。

链接的元素:

<div id="grid" data-role="grid" class="k-grid k-widget" style="opacity: 1;"><div class="k-grid-header" style="padding-right: 17px;"><div class="k-grid-header-wrap k-auto-scrollable"><table role="grid"><colgroup><col style="width:100px"><col style="width:210px"><col><col><col style="width:120px"></colgroup><thead role="rowgroup"><tr role="row"><th role="columnheader" data-field="PropertyQuickRefID" rowspan="1" data-title="PROPERTY ID" data-index="0" id="68a98aa1-147e-47f5-8eb6-1bb59bc5f37e" class="k-header" data-role="columnsorter"><a class="k-link" href="#">PROPERTY ID</a></th><th role="columnheader" data-field="PropertyNumber" rowspan="1" data-title="ACCOUNT" data-index="1" id="d4f307be-0d97-41cd-8e04-869fd5a041f4" class="k-header" data-role="columnsorter"><a class="k-link" href="#">ACCOUNT</a></th><th role="columnheader" data-field="PartyQuickRefID" rowspan="1" data-title="OWNER ID" data-index="2" id="1a4c6015-4851-4c78-9a67-1e3f2f384b00" style="display:none" class="k-header" data-role="columnsorter"><a class="k-link" href="#">OWNER ID</a></th><th role="columnheader" data-field="OwnerName" rowspan="1" data-title="OWNER NAME" data-index="3" id="206614df-19d6-4ae4-a27a-496bdda92fb3" class="k-header" data-role="columnsorter"><a class="k-link" href="#">OWNER NAME</a></th><th role="columnheader" data-field="SitusAddress" rowspan="1" data-title="SITUS ADDRESS" data-index="4" id="5cba12e3-5dde-4d67-b664-2832adf92c29" class="k-header" data-role="columnsorter"><a class="k-link" href="#">SITUS ADDRESS</a></th><th role="columnheader" data-field="LegalDescription" rowspan="1" data-title="LEGAL DESCRIPTION" data-index="5" id="3199fe1c-1ee1-4f87-87ff-5effca8a9cfe" style="display:none" class="k-header" data-role="columnsorter"><a class="k-link" href="#">LEGAL DESCRIPTION</a></th><th role="columnheader" data-field="NeighborhoodCode" rowspan="1" data-title="NEIGHBORHOOD CODE" data-index="6" id="c4b711f3-e793-45f3-b086-c95f9215a66f" style="display:none" class="k-header" data-role="columnsorter"><a class="k-link" href="#">NEIGHBORHOOD CODE</a></th><th role="columnheader" data-field="Abstract" rowspan="1" data-title="ABSTRACT" data-index="7" id="135291e9-f5e8-4d3e-9c91-50c90bf6f339" style="display:none" class="k-header" data-role="columnsorter"><a class="k-link" href="#">ABSTRACT</a></th><th role="columnheader" data-field="Subdivision" rowspan="1" data-title="SUBDIVISION" data-index="8" id="3951acbf-89f5-4f55-902c-0c55e78d124f" style="display:none" class="k-header" data-role="columnsorter"><a class="k-link" href="#">SUBDIVISION</a></th><th role="columnheader" data-field="PropertyType" rowspan="1" data-title="PROPERTY TYPE" data-index="9" id="d71ff4cd-d5fb-4b9c-9f7f-17f4ce0a2154" style="display:none" class="k-header" data-role="columnsorter"><a class="k-link" href="#">PROPERTY TYPE</a></th><th role="columnheader" data-field="PropertyValue" rowspan="1" data-title="2017 VALUE" data-index="10" id="feb614c1-9ee4-4653-bd92-d5b411bf255e" class="k-header">2017 VALUE</th></tr></thead></table></div></div><div class="k-grid-content k-auto-scrollable"><table role="grid" data-role="selectable" class="k-selectable" style="touch-action: none;"><colgroup><col style="width:100px"><col style="width:210px"><col><col><col style="width:120px"></colgroup><tbody role="rowgroup"><tr data-uid="f6c50847-b17b-4f32-9377-5386deabce48" role="row" class="rowHover"><td role="gridcell">R016698</td><td role="gridcell">R-13-0410-0620-50000</td><td style="display:none" role="gridcell">O0485204</td><td role="gridcell">GOOCH, PHILIP L</td><td role="gridcell">319 LIZZIE ST, TAYLOR, TX  76574</td><td style="display:none" role="gridcell">DOAK ADDITION, BLOCK 62, LOT 5</td><td style="display:none" role="gridcell">T541</td><td style="display:none" role="gridcell"> </td><td style="display:none" role="gridcell">S3564 - Doak Addition</td><td style="display:none" role="gridcell">Real</td><td role="gridcell"><div style="text-align:right;width:100%">$46,785</div></td></tr></tbody></table></div><div class="k-pager-wrap k-grid-pager k-widget k-floatwrap" data-role="pager"><a href="#" title="Go to the first page" class="k-link k-pager-nav k-pager-first k-state-disabled" data-page="1" tabindex="-1"><span class="k-icon k-i-seek-w">Go to the first page</span></a><a href="#" title="Go to the previous page" class="k-link k-pager-nav  k-state-disabled" data-page="1" tabindex="-1"><span class="k-icon k-i-arrow-w">Go to the previous page</span></a><span class="k-pager-input k-label">Page<input class="k-textbox">of 1</span><a href="#" title="Go to the next page" class="k-link k-pager-nav  k-state-disabled" data-page="1" tabindex="-1"><span class="k-icon k-i-arrow-e">Go to the next page</span></a><a href="#" title="Go to the last page" class="k-link k-pager-nav k-pager-last k-state-disabled" data-page="1" tabindex="-1"><span class="k-icon k-i-seek-e">Go to the last page</span></a><span class="k-pager-sizes k-label"><span title="" class="k-widget k-dropdown k-header" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-owns="" aria-disabled="false" aria-readonly="false" aria-busy="false" aria-activedescendant="8334826b-15aa-41ed-976d-52d896a5ac8a" style=""><span unselectable="on" class="k-dropdown-wrap k-state-default"><span unselectable="on" class="k-input">20</span><span unselectable="on" class="k-select"><span unselectable="on" class="k-icon k-i-arrow-s">select</span></span></span><select data-role="dropdownlist" style="display: none;"><option value="10">10</option><option value="15">15</option><option value="20">20</option><option value="50">50</option><option value="100">100</option></select></span>items per page</span><a href="#" class="k-pager-refresh k-link" title="Refresh"><span class="k-icon k-i-refresh">Refresh</span></a><span class="k-pager-info k-label">1 - 1 of 1 items</span></div></div>

我正在尝试的脚本:

Sub Get_Page()
    Dim IE As New InternetExplorer, html As HTMLDocument

    With IE
        .Visible = True
        .navigate "link"
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set html = .Document
    End With

   html.getElementsByClassName("k-selectable")(1).Click
   Application.Wait (Now + TimeValue("0:00:05"))

End Sub

这是The link,将放在我的脚本中。

这是用铅笔标记的链接的图像:

enter image description here

当我使用硒和css selector做同样的事情时它会起作用:

Sub Trigger_Click()
  Dim driver As New ChromeDriver, html As New HTMLDocument

  With driver
    .Get "replace_with_above_link"
    .Wait 1000
  End With

  driver.FindElementByCss(".k-selectable tr[role='row']").Click
  driver.Wait 1000

  driver.Quit
End Sub

所以我在上面的脚本中尝试使用.querySelector()html.getElementsByClassName("k-selectable")(0).Click替换html.querySelector(".k-selectable tr[role='row']").Click,但它也没有用。由于硒可以做到这一点,我仍然乐观,IE也可以这样做。

先生alecxe的建议似乎我非常接近解决方案。我已经能够使用下面粘贴的方法选择网格的第一部分以及图片,以显示我执行脚本时网格的样子。网格的选定部分看起来与其他部分不同。我现在需要做的就是点击一下。

这是kendoGrid方法的一部分:

html.parentWindow.execScript "$('#grid').data('kendoGrid').select('.k-selectable tr td:eq(0)');"

这是网格选定部分的图片:

enter image description here

这是可以完成工作的精炼脚本:

Sub Get_Page()
    Dim IE As New InternetExplorer, html As HTMLDocument

    With IE
        .Visible = True
        .navigate "replace_with_above_link"
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set html = .Document
    End With

   Application.Wait (Now + TimeValue("0:00:05"))
   html.parentWindow.execScript "$('#grid').data('kendoGrid').select('.k-selectable tr[role=row]');"
   html.queryselector(".k-selectable tr[role=row]").Click
   Application.Wait (Now + TimeValue("0:00:05"))
End Sub

致谢:获得该解决方案所有者@sir alecxe的许可,将其粘贴在工作解决方案之上。

vba excel-vba internet-explorer web-scraping click
1个回答
1
投票

当你使用k-selectable类来定位一个元素时,你得到的是table元素,但是你需要得到可点击的行--tr元素。

现在,问题是,您无法直接触发行元素节点的“单击”。看看在浏览器控制台中执行此操作时会发生什么:

> document.querySelector(".k-selectable tr[role=row]").click()
Property-Search-Result?searchtext=319 lizzie taylor:672 Uncaught TypeError: Cannot read property 'PropertyQuickRefID' of null
    at HTMLTableRowElement.<anonymous> (Property-Search-Result?searchtext=319 lizzie taylor:672)
    at HTMLDivElement.dispatch (VM45 jquery.js?cdv=282:3074)
    at HTMLDivElement.elemData.handle (VM45 jquery.js?cdv=282:2750)
    at <anonymous>:1:54

这是因为它是一个特定的"kendo UI" grid,行选择事件需要在点击本身之前发生。

以编程方式,我们可以使用this suggestion“选择”一个kendo UI网格行:

var grid = $("#grid").data("kendoGrid"); 

var rows = grid.dataSource.data(); 
var lastRowUid = rows[0].uid;

var row = grid.table.find("[data-uid=" + lastRowUid + "]");    
grid.select(row);

然后点击:

document.querySelector(".k-selectable tr[role=row]").click();

在Chrome控制台中为我工作,但我目前无法在VBA中测试它。

根据我的理解,你总是可以使用execScript用上面提供的逻辑执行javascript,例如:

© www.soinside.com 2019 - 2024. All rights reserved.