khalisizwan
New Member
- Joined
- Aug 7, 2021
- Messages
- 2
- Office Version
- 365
- Platform
- 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:0gl75']/div/span")
On Error Resume Next
Set CompanyName = Section.FindElementByXPath("//*[@id='pt1:r1:0:sv1:search:0gl75']/div/span[1]")
On Error Resume Next
Set UEN = Section.FindElementByXPath("//*[@id='pt1:r1:0:sv1:search:0gl31']/div/span")
Set Address = Section.FindElementByXPath("//*[@id='pt1:r1:0:sv1:search:0:cl1']")
Set Status = Section.FindElementByXPath("//*[@id='pt1:r1:0:sv1:search:0gl9']/div/span")
Set Industry = Section.FindElementByXPath("//*[@id='pt1:r1:0:sv1:search:0glsde37']/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
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:0gl75']/div/span")
On Error Resume Next
Set CompanyName = Section.FindElementByXPath("//*[@id='pt1:r1:0:sv1:search:0gl75']/div/span[1]")
On Error Resume Next
Set UEN = Section.FindElementByXPath("//*[@id='pt1:r1:0:sv1:search:0gl31']/div/span")
Set Address = Section.FindElementByXPath("//*[@id='pt1:r1:0:sv1:search:0:cl1']")
Set Status = Section.FindElementByXPath("//*[@id='pt1:r1:0:sv1:search:0gl9']/div/span")
Set Industry = Section.FindElementByXPath("//*[@id='pt1:r1:0:sv1:search:0glsde37']/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