简体   繁体   English

带有 Internet Explorer 的 VBA

[英]VBA with Internet Explorer

Just need to have a better understanding of using the internet explorer object in VBA.只需要更好地了解在 VBA 中使用 Internet Explorer 对象即可。 So I want to click the following button with a code as following所以我想点击下面的按钮,代码如下

<a id="DLG_VARIABLE_dlgBase_BTNOK" ct="B" st="" href="javascript:void(0);" class="urBtnEmph" ocl="sapbi_page.sendCommandArray([['TARGET_DIALOG_REF','DLG_VARIABLE',0],['BI_COMMAND_TYPE','OK',0]],event);" onkeydown="ur_Button_keypress(event);" onclick="ur_Button_click(event);" style="text-align:center;overflow:visible;">OK</a>

My code is as following -我的代码如下 -

 Dim IE As InternetExplorer 'Reference to Microsoft Internet Controls
 Set IE = New InternetExplorer
 With IE
 .Visible = True
 .Navigate2 "Somewebsite"

 .Document.getelemetbyclass("urBtnEmph").Click

 End With

End Sub

but when I try to run it, it says但是当我尝试运行它时,它说

'Object doesn't support this property or method'. '对象不支持此属性或方法'。

I'm very new to this internet explorer object and javascript :(我对这个 Internet Explorer 对象和 javascript 很陌生:(

Edit: I had changed the line to ' .Document.getelementsbyclass("urBtnEmph").Click' however still have the same warning.编辑:我已将行更改为 '.Document.getelementsbyclass("urBtnEmph").Click' 但是仍然有相同的警告。

Then I tried this '.Document.getElementById("DLG_VARIABLE_dlgBase_BTNOK").Click' and it shows 'Object variable or with block variable not set'.然后我尝试了这个'.Document.getElementById("DLG_VARIABLE_dlgBase_BTNOK").Click',它显示'对象变量或块变量未设置'。 I had attached the html code for this button我附上了这个按钮的 html 代码

在此处输入图片说明

Please check the Common VBA Methods & Properties used in web automation , you could use the getElementsByClassName or getElementById method to access web page element.请检查Web 自动化中使用通用 VBA 方法和属性,您可以使用getElementsByClassNamegetElementById方法来访问网页元素。

When using the getElementsByClassName method, it will return multiple elements with the same class name, so, if we want to get the special element, we could find it from the array list.当使用getElementsByClassName方法时,它会返回多个具有相同类名的元素,因此,如果我们想要获取特殊元素,我们可以从数组列表中找到它。

So, please modify your code as below:因此,请修改您的代码如下:

Public Sub ClickTest()

    Dim ie As Object

    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .Navigate2 "<website url>"

        While .Busy Or .readyState <> 4: DoEvents: Wend 

        'ie.Document.getElementbyId("DLG_VARIABLE_dlgBase_BTNOK").Click  // it also work well. find element by id 

        ie.Document.getElementsByClassName("urBtnEmph")(0).Click

    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM