Selenium Excel VBA data Scrapping

khalisizwan

New Member
Joined
Aug 7, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
In need of some help.
Im doing a data scrapping but its output only print out the first search. The code works so far that the elements of the main result will print out correctly but the result for the individual element only print the data for the first search.

code below:
Option Explicit

Sub GetDataFromACRA()
Dim Chrome As selenium.ChromeDriver
Dim FindBy As selenium.By

' With Application
' .Calculation = xlCalculationManual
' .EnableEvents = False
' .ScreenUpdating = False
' End With

'Open Browser
Set Chrome = New selenium.ChromeDriver
Chrome.Start
Chrome.get "https://www.tis.bizfile.gov.sg/ngbt...ansactionMain.jspx?selectedETransId=dirSearch"
Range("A2").Select

Do Until IsEmpty(ActiveCell)

'Define Wookbook.sheet
Worksheets("Extract").Activate
Rows(ActiveCell.Row).Select

'Function
Dim Cell As Range
Dim Row As Range
Set Cell = ActiveCell
Set Row = Row

Dim Results As selenium.WebElements
Dim Section As selenium.WebElement
Dim CompanyName As selenium.WebElement
Dim UEN As selenium.WebElement
Dim Address As selenium.WebElement
Dim Status As selenium.WebElement
Dim Industry As selenium.WebElement

'Set to Input text field
Chrome.FindElementById("pt1:r1:0:sv1:it1::content").SendKeys (ActiveCell)
Chrome.FindElementById("pt1:r1:0:sv1:cb1").Click
Application.Wait (Now + TimeValue("0:00:03"))
Dim text As Range
Set Results = Chrome.FindElementsById("pt1:r1:0:sv1:sdi1")

For Each Section In Results
'Get Section
Set CompanyName = Section.FindElementByXPath("//*[@id='pt1:r1:0:sv1:search:0:pgl75']/div/span")
On Error Resume Next
Set CompanyName = Section.FindElementByXPath("//*[@id='pt1:r1:0:sv1:search:0:pgl75']/div/span[1]")
On Error Resume Next
Set UEN = Section.FindElementByXPath("//*[@id='pt1:r1:0:sv1:search:0:pgl31']/div/span")
Set Address = Section.FindElementByXPath("//*[@id='pt1:r1:0:sv1:search:0:cl1']")
Set Status = Section.FindElementByXPath("//*[@id='pt1:r1:0:sv1:search:0:pgl9']/div/span")
Set Industry = Section.FindElementByXPath("//*[@id='pt1:r1:0:sv1:search:0:pglsde37']/div/span")

text(ActiveCell.Row - 1, ("C")).Value = UEN.text
Debug.Print UEN.text
text(ActiveCell.Row - 1, ("B")).Value = CompanyName.text
Debug.Print CompanyName.text
text(ActiveCell.Row - 1, ("G")).Value = Industry.text
Debug.Print Industry.text
text(ActiveCell.Row - 1, ("F")).Value = Address.text
Debug.Print Address.text
text(ActiveCell.Row - 1, ("D")).Value = Status.text
Debug.Print Status.text

' Debug.Print Section.Text

Chrome.FindElementById("pt1:r1:0:sv1:it1::content").Clear
Application.Wait (Now + TimeValue("0:00:02"))
ActiveCell.Offset(1, 0).Select

Next Section
Loop

End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Set Industry = Section.FindElementByXPath("//*[@id='pt1:r1:0:sv1:search:0:Pglsde37']/div/span")
 

Attachments

  • Sample.PNG
    Sample.PNG
    51.3 KB · Views: 48
Upvote 0

Forum statistics

Threads
1,215,143
Messages
6,123,277
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top