VBA:如何在HTML表格中插入值?

时间:2019-01-13 10:57:28

标签: html excel vba web-scraping html-table

我找不到伪造具有动态ID的html表笨拙的方法- 每当我打开新会话时,id都会更改。

如果我手动用“ 1”(小时)填充列(1/10/2019)-在下一个会话中,列(1/10/2019)的ID变为常量,我可以使用“ application.sendkeys(例如,将“ 7”)更改为“ 7”(小时)。

如何填充html表中以前没有手动插入任何值的列?处理此任务的正确方法是什么?

Sub Treport()

'Make the app work faster?
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'--------------------------------
Dim sht As Worksheet
Set sht = ThisWorkbook.Sheets("Report_Time") 'my data will be stored here
'--------------------------------
Dim LastRow As Long
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row 'range definition
'--------------------------------
Dim i As Long            'Will be used for a loop that navigate to different url
For i = 2 To LastRow     'variable range
Dim IE As Object         'Internet Explorer declaration
Set IE = CreateObject("InternetExplorer.Application") 'Opens browser
IE.Visible = True

IE.navigate sht.Range("A" & i).Value 'My url that I want to navigate to
While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend 'wait until site fully loads

Dim Doc As New HTMLDocument 'Will be used for the main html page
Set Doc = IE.document

Doc.getElementById("imgLastWeek").Click 'Here I click to navigate within the timesheet to the desired time range
While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend 'wait untill page loads again
Doc.getElementById("9da306a8-b813-46ff-b94f-45636f401ba8").Click 'need to click the column before inseting a value
Application.Wait Now + TimeValue("00:00:2")
Application.SendKeys ("7") 'here I change value from "1" to "7" in a clumn that I have manualy inserted number before, that is why the id is constant

Next i

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

我希望根据任务ID +列日期在列中插入小时。如果需要更多的html来了解表的结构,请提出建议,我将复制更多的代码。

<table id="tblTimeSheet_tblMain" style="width: 704px; padding-bottom: 0px;" cellspacing="0" s="">
  <colgroup id="tblTimeSheet_cgMn">
    <col style="width: 40px;" span="1" u="px" orgwidth="40">
    <col style="width: 40px;" span="1" u="px" orgwidth="40">
    <col style="width: 93px;" span="1" u="px" orgwidth="93">
    <col style="width: 35px;" span="1" u="px" orgwidth="35">
    <col style="width: 35px;" span="1" u="px" orgwidth="35">
    <col style="width: 35px;" span="1" u="px" orgwidth="35">
    <col style="width: 35px;" span="1" u="px" orgwidth="35">
    <col style="width: 35px;" span="1" u="px" orgwidth="35">
    <col style="width: 35px;" span="1" u="px" orgwidth="35">
    <col style="width: 35px;" span="1" u="px" orgwidth="35">
    <col style="width: 40px;" span="1" u="px" orgwidth="40">
  </colgroup>
  <tbody>
    <tr id="0af6368f-79e0-49f8-bbca-15808c556655" style="height: 25px;" s="" pid="1f0305b4-8683-4c61-b85e-3b0b885b6e30">
      <td align="left" class="r" ct="" s="" dp="2" ah="0" d="0" dt="0" cv="" ae="0" timetype="RemainHrs">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="left" class="r" ct="" s="" dp="2" ah="0" d="0" dt="0" cv="" ae="0" timetype="PerComp">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="center" class="r" ct="" s="" dp="2" ah="0" d="0" dt="4" cv="" ae="0" timetype="ForecastFinish">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="right" id="9ab38f91-cd6b-432e-83b2-0033796f7d54" style="background-color: rgb(238, 238, 238);" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/4/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="right" id="a7f1f9d2-b9c5-48c8-bc19-7e1d0041d1aa" style="background-color: rgb(238, 238, 238);" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/5/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="right" id="edf44c3c-b10e-4cf3-ad91-b91c08662229" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/6/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="right" id="b9cd0b45-f864-4003-ac47-5701489c8c3f" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/7/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="right" id="00ed8598-26bb-4a10-8481-64ea40149765" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/8/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td title="Description: &#10;&#13;OT hours: 0.00&#10;&#13;" align="right" class="r" id="806751c3-c2e5-40e2-b269-a0cc99ab18fe" style="color: rgb(128, 128, 144);" ct="1.00" s="" dp="2" ah="0" d="0" dt="3" cv="1.00" ae="0" celldate="1/9/2019" controltype="etbl"
        timetype="reg" hasdatarec="1">
        <nobr>1.00</nobr>
      </td>
      <td align="right" id="26557391-224c-48e5-87a3-bf002793f4e0" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/10/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="right" ct="1.00" s="" dp="2" ah="0" d="0" dt="0" cv="1.00" timetype="total" othours="0.00" reghours="1.00">
        <nobr>1.00</nobr>
      </td>
    </tr>
    <tr id="718f6602-c9fe-4cb1-a309-e8342c34a0f2" style="height: 25px;" s="" pid="d9aca3f0-04ae-44ee-94f0-c2ed14da8e24">
      <td align="left" ct="10.00" s="" dp="2" ah="0" d="0" dt="0" cv="10.00" timetype="RemainHrs">
        <nobr>10.00</nobr>
      </td>
      <td align="left" ct="0.00" s="" dp="2" ah="0" d="0" dt="0" cv="0.00" timetype="PerComp">
        <nobr>0.00</nobr>
      </td>
      <td title="1/7/2019" align="center" ct="1/7/2019" s="" dp="2" ah="0" d="0" dt="4" cv="1/7/2019" timetype="ForecastFinish">
        <nobr>1/7/2019</nobr>
      </td>
      <td align="right" id="57e0de52-1114-4532-8088-5902d01005e2" style="background-color: rgb(238, 238, 238);" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/4/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="right" id="ca52dd3e-52a3-4139-80be-ef7cbdd533dc" style="background-color: rgb(238, 238, 238);" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/5/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="right" id="62d73cbd-ede0-4043-b8ae-4d43cc65c8f4" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/6/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td title="Description: &#10;&#13;OT hours: 0.00&#10;&#13;Is work performed Onsite?: &#10;&#13;Technical Activity Bucket: &#10;&#13;Is this Travel time?: &#10;&#13;" align="right" class="r" id="4db5269d-1512-4236-a2f2-6fb51c5a3892" style="color: rgb(0, 128, 0);"
        ct="10.00" s="" dp="2" ah="0" d="0" dt="3" cv="10.00" ae="0" celldate="1/7/2019" controltype="etbl" timetype="reg" hasdatarec="1">
        <nobr>10.00</nobr>
      </td>
      <td title="Task for Karin Goren" align="right" id="3c124199-04fb-4f04-841b-cef8f6c7fece" ct="4.00" s="" dp="2" ah="0" d="0" dt="3" cv="4.00" ae="1" celldate="1/8/2019" controltype="etbl" timetype="reg" hasdatarec="1">
        <nobr>4.00</nobr>
      </td>
      <td title="Task for Karin Goren" align="right" id="ee7f0b68-98d1-440c-a801-98d432a3d322" ct="5.00" s="" dp="2" ah="0" d="0" dt="3" cv="5.00" ae="1" celldate="1/9/2019" controltype="etbl" timetype="reg" hasdatarec="1">
        <nobr>5.00</nobr>
      </td>
      <td align="right" id="784f3cf3-b9ad-43ca-9ea8-99e7384f78de" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/10/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="right" ct="19.00" s="" dp="2" ah="0" d="0" dt="0" cv="19.00" timetype="total" othours="0.00" reghours="19.00">
        <nobr>19.00</nobr>
      </td>
    </tr>
    <tr id="e2437d6b-db12-4ec2-9724-d21e84171c18" style="height: 25px;" s="" pid="d9aca3f0-04ae-44ee-94f0-c2ed14da8e24">
      <td align="left" ct="10.00" s="" dp="2" ah="0" d="0" dt="0" cv="10.00" timetype="RemainHrs">
        <nobr>10.00</nobr>
      </td>
      <td align="left" ct="0.00" s="" dp="2" ah="0" d="0" dt="0" cv="0.00" timetype="PerComp">
        <nobr>0.00</nobr>
      </td>
      <td title="1/7/2019" align="center" ct="1/7/2019" s="" dp="2" ah="0" d="0" dt="4" cv="1/7/2019" timetype="ForecastFinish">
        <nobr>1/7/2019</nobr>
      </td>
      <td align="right" id="ed31f3c4-a5c6-4d79-ad8a-9d5debdd20dc" style="background-color: rgb(238, 238, 238);" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/4/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="right" id="4e26eafe-f2fb-4005-9ff5-190b436ce5e4" style="background-color: rgb(238, 238, 238);" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/5/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="right" id="a3cc1439-c20c-4568-afd5-62b407379da8" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/6/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="right" id="442c6007-7ed3-43ef-a29f-a659a3fa82bf" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/7/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="right" id="63972eb3-9de2-4a1b-9b41-4d0a7ff6bf28" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/8/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td title="Task for Karin Goren" align="right" id="6e87f65e-6f14-4992-9e78-ec1785188c23" ct="3.00" s="" dp="2" ah="0" d="0" dt="3" cv="3.00" ae="1" celldate="1/9/2019" controltype="etbl" timetype="reg" hasdatarec="1">
        <nobr>3.00</nobr>
      </td>
      <td align="right" id="93041572-96b4-43aa-a261-6ba53bccb519" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/10/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="right" ct="3.00" s="" dp="2" ah="0" d="0" dt="0" cv="3.00" timetype="total" othours="0.00" reghours="3.00">
        <nobr>3.00</nobr>
      </td>
    </tr>
    <tr id="f6b76a38-f33f-4ecc-8c33-99e061877560" style="height: 25px;" s="" pid="bd3bedb4-dbba-4132-a61c-7357765eb80d">
      <td align="left" ct="20.00" s="" dp="2" ah="0" d="0" dt="0" cv="20.00" timetype="RemainHrs">
        <nobr>20.00</nobr>
      </td>
      <td align="left" ct="0.00" s="" dp="2" ah="0" d="0" dt="0" cv="0.00" timetype="PerComp">
        <nobr>0.00</nobr>
      </td>
      <td title="1/7/2019" align="center" ct="1/7/2019" s="" dp="2" ah="0" d="0" dt="4" cv="1/7/2019" timetype="ForecastFinish">
        <nobr>1/7/2019</nobr>
      </td>
      <td align="right" id="5161894a-6047-442c-9896-e81f90230238" style="background-color: rgb(238, 238, 238);" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/4/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="right" id="0f7cb1c2-67ae-4c75-b094-dd083911d1c9" style="background-color: rgb(238, 238, 238);" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/5/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="right" id="d11cc9dd-6f21-4a43-acbd-de48224df366" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/6/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td align="right" id="ce4ef478-1b4d-436a-97dd-66fcffca6611" ct="" s="" dp="2" ah="0" d="0" dt="3" cv="" ae="1" celldate="1/7/2019" controltype="etbl" timetype="reg" hasdatarec="0">
        <nobr>&nbsp;</nobr>
      </td>
      <td title="Description: &#10;&#13;OT hours: 0.00&#10;&#13;Is work performed Onsite?: &#10;&#13;Technical Activity Bucket: &#10;&#13;Is this Travel time?: &#10;&#13;" align="right" class="r" id="d0151d04-d2f1-4413-b6bb-d3560f95c0f5" style="color: rgb(128, 128, 144);"
        ct="1.00" s="" dp="2" ah="0" d="0" dt="3" cv="1.00" ae="0" celldate="1/8/2019" controltype="etbl" timetype="reg" hasdatarec="1">
        <nobr>1.00</nobr>
      </td>
      <td title="Description: &#10;&#13;OT hours: 0.00&#10;&#13;Is work performed Onsite?: &#10;&#13;Technical Activity Bucket: &#10;&#13;Is this Travel time?: &#10;&#13;" align="right" class="r" id="aba659f1-57dc-4dbd-b7ef-263a0b020da7" style="color: rgb(128, 128, 144);"
        ct="2.00" s="" dp="2" ah="0" d="0" dt="3" cv="2.00" ae="0" celldate="1/9/2019" controltype="etbl" timetype="reg" hasdatarec="1">
        <nobr>2.00</nobr>
      </td>
      <td title="Test task overlapping hours" align="right" id="9da306a8-b813-46ff-b94f-45636f401ba8" ct="1.00" s="" dp="2" ah="0" d="0" dt="3" cv="1.00" ae="1" celldate="1/10/2019" controltype="etbl" timetype="reg" hasdatarec="1">
        <nobr>1.00</nobr>
      </td>
      <td align="right" ct="4.00" s="" dp="2" ah="0" d="0" dt="0" cv="4.00" timetype="total" othours="0.00" reghours="4.00">
        <nobr>4.00</nobr>
      </td>
    </tr>
  </tbody>
</table>

UI and HTML

1 个答案:

答案 0 :(得分:1)

使用IE浏览器时,您可以访问范围更广的选择器,包括一些pseudo class css selectors,例如nth-of-type(最后我触摸:last-child)。这与通过.querySelector使用HTMLDocument对象的MSHTML方法可以应用的方法相反。

因此,对于所显示的HTML,您可以使用nth-of-type选择器来抓取感兴趣的行(显示红色和蓝色矩形的行),以tr标签选择第n个出现的行,父表由ID标识(#是id selector)。

让我们看看根据返回的元素进行匹配时该行的外观:

enter image description here

应该类似(请记住,在我添加td元素选择器之前,不会显示空的td元素-下一步显示):

enter image description here

现在,让我们细分一下,使用后代组合器和td元素选择器在该行中包括子元素td(表单元):

enter image description here

在上面,您现在可以查看该行内的所有表格单元格,并使用您看到的属性,位置或两者的组合来定位感兴趣的元素(表格单元格)。

例如,如果您提前知道红色星期一7号的日期,则可以使用celldate属性定位并将其添加到选择器链中:

#tblTimeSheet_tblMain tr:nth-of-type(4) td[celldate='1/7/2019']

enter image description here

如果不知道日期,则可以再次使用位置,并在nth-of-type行的td单元格中使用tr索引(请注意,这是base-1而不是0):

#tblTimeSheet_tblMain tr:nth-of-type(4) td:nth-of-type(7)

enter image description here

这很像通过xy列语法进行引用。

上面显示的css选择器通过.querySelector的{​​{1}}方法来应用,例如

ie.document

ie.document.querySelector("#tblTimeSheet_tblMain tr:nth-of-type(4) td:nth-of-type(7)").value = "xyx"

我无法使用浏览器和vba库对此进行测试,但我知道ie.document.querySelector("#tblTimeSheet_tblMain tr:nth-of-type(4) td:nth-of-type(7)").innerText = "xyx" 以及attribute = value selectorsdescendant combinators均受支持。

在这种情况下,应该知道还支持last-child选择器,并且在引用最后nth-of-type行时,还可以使用:

tr

其他注意事项:

  1. 选择器越长,选择器将越慢。尽管使用现代浏览器,您在谈论的性能差异很小。
  2. 此外,最右边的是键选择器,并且应该尽可能地具有选择性,例如id选择器(如果可能),否则类选择器...选择的顺序已记录在案。所有这些都应该尽可能有选择性,但是记住从右到左的阅读很重要。
  3. #tblTimeSheet_tblMain tr:last-child td:nth-of-type(7) 应该类似于以下链接:#tblTimeSheet_tblMain tr:nth-of-type(4) td:nth-of-type(7),但性能更高。

有关性能和选择器选择here的更多信息。摘录: