Advertisement
jdelano

Untitled

Jun 25th, 2025
1,663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Private Sub btnGetInfo_Click()
  2.  
  3.     ' loop the table to find the information for eac registration
  4.    Dim sheetRow As Long
  5.     Dim siteURL As String
  6.     Dim registration As String
  7.     Dim lastRow As Long
  8.    
  9.     Dim listDIVs As WebElements  ' all the div elements inside where the Registration / Fleet Number info is
  10.    Dim listDiv As WebElement    ' each div
  11.    Dim anchorElement As WebElement  ' the element that contains the anchor to click (its text equals col A in the sheet)
  12.    
  13.     Dim subDIVs As WebElements   ' all the div elements showing the data after clicking the registration
  14.    
  15.     Dim cDriver As ChromeDriver
  16.    
  17.     ' create a chomedriver object
  18.    Set cDriver = New ChromeDriver
  19.     siteURL = "https://www.planespotters.net/search?q="
  20.    
  21.     lastRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row ' which row is the last one used
  22.    
  23.     ' loop the data on the sheet - change the 5 to whatever the data starts in the future
  24.    For sheetRow = 5 To lastRow
  25.         registration = Sheet1.Cells(sheetRow, "A").Value
  26.        
  27.         ' open Chrome and go to the specified address
  28.        cDriver.Get siteURL & registration
  29.         cDriver.Wait 2000 ' wait 2 seconds for the page to load
  30.        
  31.         ' find where the registration anchor is (clickable link), this is found by using
  32.        ' the inspect option in the right click menu in chrome when viewing the webpage
  33.        Set listDIVs = cDriver.FindElementsByClass("dt-tr")(2).FindElementsByTag("div")
  34.        
  35.         For Each listDiv In listDIVs
  36.             If listDiv.FindElementByTag("a").Text <> "" Then
  37.                 ' this is a div that contains a link to click
  38.                Set anchorElement = listDiv.FindElementByTag("a")
  39.                
  40.                 ' if the link text matches the registration info from teh sheet then leave the loop
  41.                If anchorElement.Text = registration Then Exit For
  42.             End If
  43.        
  44.         Next listDiv
  45.        
  46.         ' click the link that was found above
  47.        anchorElement.Click
  48.         cDriver.Wait 2000 ' wait 2 seconds for the page to load
  49.      
  50.         ' get the divs that make up the location of the data in the page
  51.        On Error Resume Next
  52.         Set subDIVs = cDriver.FindElementByClass("flex-column").FindElementsByClass("dt-tr")
  53.        
  54.         If Err <> 0 Then
  55.             ' there may be an advert infront of the data that needs to be cleared before moving on
  56.            MsgBox "The webpage isn't displaying corrently. Click close on the advertisement then click OK on this message to continue", vbOKOnly, "Page Issue"
  57.             Set subDIVs = cDriver.FindElementByClass("flex-column").FindElementsByClass("dt-tr")
  58.         End If
  59.         Err = 0
  60.         On Error GoTo 0
  61.        
  62.         Sheet1.Cells(sheetRow, "D").Value = SearchDIVs("MSN", subDIVs)                           ' MSN
  63.        Sheet1.Cells(sheetRow, "C").Value = SearchDIVs("Line Number", subDIVs)                   ' Line Number
  64.        Sheet1.Cells(sheetRow, "B").Value = SearchDIVs("Aircraft Type", subDIVs)                 ' aircraft type
  65.        Sheet1.Cells(sheetRow, "E").Value = cDriver.FindElementByTag("h1").Text                  ' airline/company
  66.        
  67.         ' remove the aircraft type from the airline company text at the top of the page
  68.        Sheet1.Cells(sheetRow, "E").Value = Replace(Sheet1.Cells(sheetRow, "E").Value, UCase(Sheet1.Cells(sheetRow, "B").Value), "")
  69.        
  70.         Set subDIVs = Nothing
  71.        
  72.         DoEvents
  73.     Next sheetRow
  74.    
  75.     Set listDIVs = Nothing
  76.    
  77.     cDriver.Close
  78.     Set cDriver = Nothing
  79.    
  80.     MsgBox "Done."
  81.    
  82. End Sub
  83.  
  84. Private Function SearchDIVs(valueTitle As String, subDIVs As WebElements) As String
  85.  
  86.     ' go through each div text in the list of divs passed.
  87.    ' find the value associated to the title
  88.    
  89.     Dim subDiv As WebElement
  90.     Dim titleDiv As WebElement
  91.     Dim valueDiv As WebElement
  92.    
  93.     For Each subDiv In subDIVs
  94.         ' get the data from the 2 divs inside the list div
  95.        Set titleDiv = subDiv.FindElementsByTag("div")(1)
  96.         Set valueDiv = subDiv.FindElementsByTag("div")(2)
  97.        
  98.         If InStr(titleDiv.Text, valueTitle) > 0 Then
  99.             If valueTitle = "Aircraft Type" Then
  100.                 ' the aircraft type has an unordered list with a list element inside it
  101.                ' unlike the other items wanted from the web page
  102.                SearchDIVs = subDiv.FindElementsByTag("li")(2).Text
  103.             Else
  104.                 SearchDIVs = valueDiv.Text
  105.             End If
  106.            
  107.             Exit Function
  108.         End If
  109.    
  110.     Next subDiv
  111.  
  112.     SearchDIVs = ""
  113. End Function
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement