1

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

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

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

I have tried following code:

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

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

Thanks in advance

5
  • what you are doing looks pretty painful, have you considered webdriver through VBA...guru99.com/excel-vba-selenium.html Commented Feb 8, 2021 at 20:54
  • Could you be explicit about the selections you are making prior to trying to select the all notifications? It looks like you want to hit the Former KAP Member Companies and then select a company a long way day that dropdown list. Commented Feb 9, 2021 at 6:20
  • First of all thanks for the comments, I didint expect getting such a fast response:) I am trying to do a very basic set of operations. In order to reach out to the search results that I am looking for, I need to select three criteria in order to get the results when I click to '''Search''' button below. 1) I need to click the "All notifications" under the "Notification Type" checkbox. This selection triggers the second checkbox ("which is Subject"). After clicking the "All Notifications" button, the values in "Subject" show up. Commented Feb 9, 2021 at 18:33
  • After selection of two subjects, I will set time interval (hopefully I will try to tie that up a cell value in excel main page, so whenever I try to look for values in different date interval, I will be able to control that form my excel interface") Commented Feb 9, 2021 at 18:37
  • Thanks for the advice @Ctznkane525 I will try that. Commented Feb 9, 2021 at 18:41

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response, I will try that and write back for the results
As I understood from your comments, the timing issue is emerging from the first click to the drop down to open menu because after clicking it didn't respond immediately and because i didn't respond quickly, there was nothing to click in the sibling nodes, so clicking the sibling object did not work right? Just asking for minfding my mind.
I did tried the code above and it work, also replicated it to 'Subjects' drop down and it also work although it is not quite refined. I will post the code as picture because of character limit. imgur.com/a/BqTNYDd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.