【问题标题】:How can I click a java based web button with VBA?如何使用 VBA 单击基于 Java 的 Web 按钮?
【发布时间】:2021-02-09 07:17:06
【问题描述】:

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

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

在搜索项目之前,我首先需要在下方的多选按钮中输入一些条件。这就是我的问题开始的地方。我正在尝试单击位于“通知类型”下的“所有通知”选项卡。但是有些我无法做到这一点。 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 和所有这些东西的初学者,我被困住了。如果有人可以帮助我,我将不胜感激。

提前致谢

【问题讨论】:

  • 你在做的事情看起来很痛苦,你有没有考虑过通过VBA的webdriver...guru99.com/excel-vba-selenium.html
  • 在尝试选择all notifications 之前,您能否明确说明您所做的选择?看起来您想点击Former KAP Member Companies,然后在该下拉列表中选择一家公司。
  • 首先感谢 cmets,我没想到会得到这么快的响应:) 我正在尝试做一组非常基本的操作。为了获得我正在寻找的搜索结果,我需要选择三个条件才能在单击下面的“搜索”按钮时获得结果。 1)我需要点击“通知类型”复选框下的“所有通知”。此选择触发第二个复选框(“这是主题”)。单击“所有通知”按钮后,“主题”中的值会显示出来。
  • 选择两个科目后,我将设置时间间隔(希望我会尝试将其绑定到 excel 主页中的单元格值,所以每当我尝试查找不同日期间隔中的值时,我将能够控制该表单我的 excel 界面")
  • 感谢@Ctznkane525 的建议,我会尝试的。

标签: jquery excel vba web-scraping click


【解决方案1】:

您的代码中存在时间问题。你可以用一个循环来解决它。除此之外,我优化了代码并从早期绑定切换到后期绑定。那么就没有必要将绑定设置为HTML 对象库Internet 控件。但 IntelliSense 不适用于后期绑定。

代码中有一些 cmets 供你参考:

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

【讨论】:

  • 感谢您的回复,我会尝试并回复结果
  • 正如我从您的 cmets 中了解到的,时间问题从第一次单击到下拉菜单以打开菜单出现,因为单击后它没有立即响应并且因为我没有快速响应,所以在兄弟节点中没有什么可以点击的,所以点击兄弟对象不起作用?只是要求头脑清醒。
  • 我确实尝试了上面的代码并且它可以工作,也将它复制到“主题”下拉列表中并且它也可以工作,尽管它不是很精致。由于字数限制,我将代码作为图片发布。 imgur.com/a/BqTNYDd
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-12
相关资源
最近更新 更多