提示对象不支持他的属性或方法

时间:2019-08-07 09:09:23

标签: html vba web-scraping internet-explorer-11

我尝试在选择左侧部分的按钮1之后更新右侧部分的字段1,但是系统提示运行时错误438代码。

我尝试更改代码最后一行的元素和属性,但似乎无济于事。

以下是我的VBA脚本的一部分:

Sub BrowseToWebTest1()

Dim ie As Object
Dim the_button_elements As Object
Dim button_element As Object
Dim radioButton As Object
Dim radioButtons As Object
Dim doc As HTMLDocument
Set ie = New InternetExplorerMedium

ie.navigate "company system web"
ie.Visible = True

While ie.Busy
DoEvents
Wend

Set doc = ie.document

Set the_button_elements = doc.getElementsByTagName("button")
For Each button_element In the_button_elements
    If button_element.getAttribute("onclick") = "CreateAcqCase();" Then
        button_element.Click
        Exit For
    End If
Next button_element

Call doc.getElementByName(“TransactionID”).setAttribute(“value”, “test”)

下面是DOM Explorer代码:

<input name="$PAcqCaseCreation$pTransactionID" class="leftJustifyStyle" id="TransactionID" style="width: 175px;" type="text" maxlength="15" value="" data-ctl='["TextInput"]' minchars="15" validationtype="minchars" data-changed="false">

希望有人致电帮助,以便我可以相应地更新字段。顺便说一下,我正在使用IE11和Window 10

1 个答案:

答案 0 :(得分:0)

1)您在这里有一个错误:

doc.getElementByName(“TransactionID”).setAttribute(“value”, “test”)

方法为getElementsByName,请注意s表示复数-返回一个集合。由于是集合,因此您需要提供适当的索引以定位感兴趣的元素。

2)另外,您还引入了智能的“您想要的地方”。

3)不需要call关键字,也不需要括号。

4)name属性是:

name="$PAcqCaseCreation$pTransactionID" 

id属性为:

id="TransactionID"

id可能是唯一的,并且是更好的选择器(并且是单数,因此没有s或索引):

doc.getElementId("TransactionID").setAttribute "value", "test"

否则,

doc.getElementsByName("$PAcqCaseCreation$pTransactionID")(0).setAttribute "value", "test" 

这将假设collection中的第一个元素是正确的;否则,请更改索引。

5)您可以替换所有这些(并删除关联的声明):

Set the_button_elements = doc.getElementsByTagName("button")
For Each button_element In the_button_elements
    If button_element.getAttribute("onclick") = "CreateAcqCase();" Then
        button_element.Click
        Exit For
    End If
Next button_element

仅一行:

doc.querySelector("[onclick='CreateAcqCase();']").Click
相关问题