如何使用VBA单击基于Java的Web按钮? [英] How can I click a java based web button with VBA?

查看:80
本文介绍了如何使用VBA单击基于Java的Web按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用VBA从以下网页抓取数据:

I was trying to scrape data from the following webpage using VBA:

https://www.kap.org.tr/tr/bildirim-sorgu

在搜索项目之前,我首先需要向下方的多重选择按钮输入一些条件.那就是我的问题开始的地方.我试图点击所有通知"位于通知类型"下的标签.但是有些我做不到. 1

Before I search fot the items, I first need to enter some criteria to the lower side multi select buttons. That's where my problem begins. I am trying to click on "all notifications" tab which is located under "notification type". However some I couldn't achieve to do that. 1

我尝试了以下代码:

Sub VBAWebScraping()

Dim IEObject As InternetExplorer

Set IEObject = New InternetExplorer


IEObject.Visible = True


IEObject.navigate Url:="https://www.kap.org.tr/tr/bildirim-sorgu"


Do While IEObject.Busy = True Or IEObject.readyState <> READYSTATE_COMPLETE
    
    Application.Wait Now + TimeValue("00:00:01")
    
Loop

Dim KAPMainPage As HTMLDocument
Set KAPMainPage = IEObject.document

Dim Filters As IHTMLElementCollection
Set Filters = KAPMainPage.getElementsByClassName("filter-multi padding right")

Dim NotiType As IHTMLElement
Set NotiType = Filters.Item(2)

NotiType.Click

Dim cbxItems2 As IHTMLElementCollection
Set cbxItems2 = KAPMainPage.getElementsByClassName("multiSelectItem vertical")


Dim NButton As Object
Set NButton = cbxItems2.Item(925)

NButton.Click

IEObject.Visible = False
End Sub

我是VBA的初学者,而所有这些东西我都被卡住了.我感谢有人能帮助我.

I am a beginner in VBA and all this stuff and I'm stucked. I appreciate if somebody could help me.

预先感谢

推荐答案

您的代码中存在计时问题.您可以通过循环解决它.在此之上,我优化了代码,并从早期绑定切换为后期绑定.这样就不必将绑定设置为 HTML对象库 Internet控件.但是 IntelliSense 不适用于后期绑定.

There is a timing issue in your code. You can solve it with a loop. Over that I have optimised the code and I switched from early binding to late binding. Then it is not necessary to set the bindings to HTML Object Library and Internet Controls. But IntelliSense is not available with late binding.

代码中有一些注释供您选择:

There are some comments in the code for you:

Sub VBAWebScraping()

Const url As String = "https://www.kap.org.tr/tr/bildirim-sorgu"
Dim ie As Object
Dim nodeNotificationType As Object
Dim startTimeout As Double

  Set ie = CreateObject("InternetExplorer.Application")
  ie.Visible = True
  ie.navigate url
  'Wait for the right HTML element
  startTimeout = Timer
  Do
    'Switch off error handling
    On Error Resume Next
    'Try to catch the jQuery dropdown for the notification type
    Set nodeNotificationType = ie.document.getElementsByClassName("filter-multi padding right")(2)
    'Switch on error handling
    On Error GoTo 0
  'Try it again till the dropdown was loaded or until timeout
  Loop Until (Not nodeNotificationType Is Nothing) Or (Timer - startTimeout > 5) 'Timeout in seconds
  
  'Check wether the dropdown was loaded
  If Not nodeNotificationType Is Nothing Then
    'Click to open the dropdown
    nodeNotificationType.Click
    'Click on the first entry. That's the element with the index 0 in the node collection
    'The dropdown entries are in another element of the HTML document
    'Not in the object variable nodeNotificationType
    'It's the next HTML element in the same hierarchy level of the HTML document
    'Therefore it's the nextSibling
    nodeNotificationType.NextSibling.getElementsByClassName("multiSelectItem vertical")(0).Click
  Else
    'If nodeNotificationType is not available after timeout
    MsgBox "Page was not loaded till timeout takes effect."
  End If
End Sub

这篇关于如何使用VBA单击基于Java的Web按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆