我正在尝试使用Excel VBA
来填充web form
,然后抓取一些数据。
我坚持如何输入要搜索的值。
在源代码中,没有我可以使用的ID或名称。
我尝试了一个类名,但是它不起作用。
" Source code:
<input type="text" style="height: 28px; width: 170px; padding-left: 5px;" ng-model="model.FieldValue" placeholder="Enter a Value" ng-show="model.searchOptions.length && model.searchOptions != 'SQE Quote Id'" class="ng-pristine ng-valid ng-touched">
Sub Macro1()
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim html As HTMLDocument
Set IE = New InternetExplorerMedium
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "xxxx"
'Navigate to URL
IE.Navigate URL
' Statusbar let's user know website is loading
Application.StatusBar = URL & " is loading. Please wait..."
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.ReadyState = 4: DoEvents: Loop 'Do While
Do Until IE.ReadyState = 4: DoEvents: Loop 'Do Until
'
Set html = IE.Document
IE.Document.getElementById("searchOptions").Click (2)
IE.Document.getElementById("searchOptions").Value = "Access Qref"
IE.Document.getelemenetbyclassname("ng-pristine ng-valid ng-touched").Value = "00000"
'
End Sub
答案 0 :(得分:0)
尝试输入“。”像下面这样的班级名称之间的关系在我这边工作。
IE.Document.querySelector("input.ng-pristine.ng-valid.ng-touched").Value = "00000"
修改示例:
Sub Macro1()
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim html As HTMLDocument
Set IE = New InternetExplorerMedium
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "D:\Backup20190913\tests\353.html"
'Navigate to URL
IE.Navigate URL
' Statusbar let's user know website is loading
Application.StatusBar = URL & " is loading. Please wait..."
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.ReadyState = 4: DoEvents: Loop 'Do While
Do Until IE.ReadyState = 4: DoEvents: Loop 'Do Until
Set html = IE.Document
IE.Document.querySelector("input.ng-pristine.ng-valid.ng-touched").Value = "00000"
End Sub
输出:
答案 1 :(得分:0)
错误如前所述。该节点不存在您尝试的方法。您可以在正确的方法getElementsByClassName
内使用多值类(该元素有多个类名;由元素的class属性中用空格分隔的多个字符串表示),而不是{{1 }},那么您需要索引返回的集合才能执行getelemenetbyclassname
方法。
.click
但是,与其他答案一样,如果使用IE.Document.getElementsByClassName("ng-pristine ng-valid ng-touched")(0).Value = "00000" '0 for first node
与css class selector进行匹配,则多值类之间必须用“。”代替。更好的是,尝试在多值中选择尽可能少的那些类名,并且在所需节点上仍匹配的最有选择性的
请在SO上写很多,使用适当的页面加载等待:
querySelector/querySelectorAll