屏幕抓取 - 通过几个标签名称向下搜索到 class 个名称
Screenscraping - going down through several tag names to a class name
很难找到蓝线来给它赋值(它是一个文本框):
这是我目前的尝试 - 代码返回典型的 Run-time error '91': Object variable or With block variable not set
IE.Document.GetElementById("main"). _
GetElementsByTagName("div")(0). _
GetElementsByTagName("div")(0). _
GetElementsByTagName("div")(0). _
GetElementsByTagName("table")(0). _
GetElementsByTagName("tbody")(0). _
GetElementsByTagName("tr")(0). _
GetElementsByTagName("td")(0). _
GetElementsByClassName("textbox125 PatientID")(0).Value = patientid
自从我完成屏幕抓取以来已经有一段时间了,我猜我在那里有一个额外的标签名称,或者我在其中一个标签的错误项目编号上......在任何人问之前,我真的不喜欢查询选择器,我更喜欢这样写我的抓取:)。
运行只有这个:
Sub TestMe()
Dim objApp As Object
Set objApp = CreateObject("InternetExplorer.Application")
objApp.Navigate "www.whosebug.com"
Do While objApp.readyState <> 4
DoEvents
Loop
Dim myDocument As Object: Set myDocument = objApp.Document
Dim myElement As Object: Set myElement = myDocument.getElementById("footer")
Stop
End Sub
- 它停在
Stop
行。
- 用鼠标select这个词
myElement
,所以它变成了蓝色。
- 按Shift+F9输入本地window.
- 在本地 window 中,查看整个树并将其复制到您的代码中。
您应该可以使用:
ie.document.querySelector("#main .textbox125.PatientID")
这针对 main
的父 ID 和目标元素 class .textbox125 PatientID
。由于不允许使用复合 class 名称,因此需要额外的 .
来删除空格。
如果您不想使用 querySelector,只使用可见的,猜测会使用标签名称并执行:
ie.document.getElementsByTagName("input")(1)
记住:
您随时可以右键单击以获取元素的基本 selector/xpath
而且,您使用的路径越长,您的方法就越脆弱。
更长的路径:
据我所知,您需要 second td
标签,即索引 1,然后用子 input
标签重复该标签。这是建立在看得见的基础上的!真的需要实际的 HTML 来测试,因为我对它的可行性没有信心。
IE.Document.GetElementById("main"). _
GetElementsByTagName("div")(0). _
GetElementsByTagName("div")(0). _
GetElementsByTagName("div")(0). _
GetElementsByTagName("table")(0). _
GetElementsByTagName("tbody")(0). _
GetElementsByTagName("tr")(0). _
GetElementsByTagName("td")(1). _
GetElementsByTagName("input")(1).Value = patientid
很难找到蓝线来给它赋值(它是一个文本框):
这是我目前的尝试 - 代码返回典型的 Run-time error '91': Object variable or With block variable not set
IE.Document.GetElementById("main"). _
GetElementsByTagName("div")(0). _
GetElementsByTagName("div")(0). _
GetElementsByTagName("div")(0). _
GetElementsByTagName("table")(0). _
GetElementsByTagName("tbody")(0). _
GetElementsByTagName("tr")(0). _
GetElementsByTagName("td")(0). _
GetElementsByClassName("textbox125 PatientID")(0).Value = patientid
自从我完成屏幕抓取以来已经有一段时间了,我猜我在那里有一个额外的标签名称,或者我在其中一个标签的错误项目编号上......在任何人问之前,我真的不喜欢查询选择器,我更喜欢这样写我的抓取:)。
运行只有这个:
Sub TestMe()
Dim objApp As Object
Set objApp = CreateObject("InternetExplorer.Application")
objApp.Navigate "www.whosebug.com"
Do While objApp.readyState <> 4
DoEvents
Loop
Dim myDocument As Object: Set myDocument = objApp.Document
Dim myElement As Object: Set myElement = myDocument.getElementById("footer")
Stop
End Sub
- 它停在
Stop
行。 - 用鼠标select这个词
myElement
,所以它变成了蓝色。 - 按Shift+F9输入本地window.
- 在本地 window 中,查看整个树并将其复制到您的代码中。
您应该可以使用:
ie.document.querySelector("#main .textbox125.PatientID")
这针对 main
的父 ID 和目标元素 class .textbox125 PatientID
。由于不允许使用复合 class 名称,因此需要额外的 .
来删除空格。
如果您不想使用 querySelector,只使用可见的,猜测会使用标签名称并执行:
ie.document.getElementsByTagName("input")(1)
记住:
您随时可以右键单击以获取元素的基本 selector/xpath
而且,您使用的路径越长,您的方法就越脆弱。
更长的路径:
据我所知,您需要 second td
标签,即索引 1,然后用子 input
标签重复该标签。这是建立在看得见的基础上的!真的需要实际的 HTML 来测试,因为我对它的可行性没有信心。
IE.Document.GetElementById("main"). _
GetElementsByTagName("div")(0). _
GetElementsByTagName("div")(0). _
GetElementsByTagName("div")(0). _
GetElementsByTagName("table")(0). _
GetElementsByTagName("tbody")(0). _
GetElementsByTagName("tr")(0). _
GetElementsByTagName("td")(1). _
GetElementsByTagName("input")(1).Value = patientid