vba:如何在html表中插入值?

vba:如何在html表中插入值?,html,excel,vba,web-scraping,html-table,Html,Excel,Vba,Web Scraping,Html Table,我找不到一种方法来处理html表Clunn,它有一个动态id- 每次打开新会话时,id都会更改 如果我手动将列(1/10/2019)填充为“1”(小时)-在下一个会话中,列(1/10/2019)的id变为常量,我可以使用“application.sendkeys(“7”)”将值更改为“7”(小时) 如何在html表中填充一个我以前没有手动插入任何值的列?完成这项任务的最佳方法是什么 Sub Treport() 'Make the app work faster? Application.Scr

我找不到一种方法来处理html表Clunn,它有一个动态id- 每次打开新会话时,id都会更改

如果我手动将列(1/10/2019)填充为“1”(小时)-在下一个会话中,列(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
Sub-Treport()
“让应用程序运行得更快?
Application.ScreenUpdating=False
Application.DisplayAlerts=False
'--------------------------------
将sht变暗为工作表
Set sht=thiswook.Sheets(“报告时间”)我的数据将存储在这里
'--------------------------------
最后一排一样长
LastRow=sht.Cells(sht.Rows.Count,“A”).End(xlUp).Row'范围定义
'--------------------------------
Dim i As Long'将用于导航到不同url的循环
对于i=2至最后一行的变量范围
Dim IE作为对象的Internet Explorer声明
设置IE=CreateObject(“InternetExplorer.Application”)“打开浏览器
可见=真实
IE.navigate sht.Range(“A”&i).Value“我要导航到的url”
当IE.readyState 4或IE.Busy时:DoEvents:Wend'等待站点完全加载
Dim Doc As New HTMLDocument'将用于主html页面
Set Doc=IE.document
Doc.getElementById(“imgLastWeek”)。单击此处我单击可在时间表内导航到所需的时间范围
当IE.readyState 4或IE.Busy:DoEvents:Wend'等待页面再次加载时
文档getElementById(“9da306a8-b813-46ff-b94f-45636f401ba8”)。单击“需要在插入值之前单击列”
应用程序。立即等待+时间值(“00:00:2”)
Application.SendKeys(“7”)'在这里,我将我之前手动插入的数字中的值从“1”更改为“7”,这就是id保持不变的原因
接下来我
Application.DisplayAlerts=True
Application.ScreenUpdating=True
端接头
我希望根据任务Id+列的日期在列中插入小时。如果需要更多的html来理解表结构,请给出建议,我将复制更多的代码


1
1
10
0
1/7/2019
10
4
5
19
10
0
1/7/2019
3
3
20
0
1/7/2019
1
2
1
4

当您使用IE浏览器时,您可以访问范围更广的选择器,包括一些选择器,例如(最后,我触摸了
:最后一个孩子
)。这与通过
MSHTML
使用
HTMLDocument
对象的
.querySelector
方法可以应用的相反

因此,对于显示的HTML,您可以首先使用类型为
nth的
tr
选择器抓取感兴趣的行(以红色和蓝色矩形显示的行),以选择由id标识的父表的第n次出现的行(#是一个)

让我们看看当根据返回的元素进行匹配时,该行是什么样子的:

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

现在,让我们使用子代组合符和元素选择器将其分解为该行中的子
td
(表单元格)元素:

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

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

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

如果日期未知,您可以再次使用position,并使用
td
单元格的
nth type
索引和
tr
行(注意这是base-1而不是0):

这非常类似于通过
x
y
列语法进行引用

上面显示的css选择器是通过
.querySelector
方法应用的

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

我不能用浏览器和vba库来测试这一点,但我知道
nth类型
也受支持

在这种情况下,值得知道的是,选择器也受支持,在引用最后一行时,您还可以使用:

#tblTimeSheet_tblMain tr:last-child td:nth-of-type(7)
需要注意的其他要点:

  • 选择器越长,速度越慢。尽管现代浏览器的性能差异很小
  • 此外,最右边是键选择器,应尽可能具有选择性,例如,如果可能,id选择器,否则类选择器。。。记录了在中选择的顺序。一切都应该尽可能有选择性,但我认为
    ie.document.querySelector("#tblTimeSheet_tblMain tr:nth-of-type(4) td:nth-of-type(7)").innerText = "xyx"
    
    #tblTimeSheet_tblMain tr:last-child td:nth-of-type(7)