florentbr/SeleniumBasic

Scrape Next pages on Amazon Site

leprince2007 opened this issue · 0 comments

Hello everybody,
Could you help me in my question ,please?
The code in my file searches amazon site for a product and gets the following data "name-price-link" in order to find the cheapest offer.The code gets data of first search page only.
The question:
How to make the code navigate to other pages in Amazon to get data of products from?
VBA & Selenium

My Code:
`Private ch As New Selenium.ChromeDriver
Sub scrapnet2()

Dim FindBy As New Selenium.By:   Dim items As Selenium.WebElements
Dim item As Selenium.WebElement: Dim keys As New Selenium.keys
Dim pager As Selenium.WebElement
 Application.ScreenUpdating = False
Application.EnableEvents = False

'============================================================
'Clear Result Range
ThisWorkbook.Sheets("Data").Activate
a = [a10000].End(xlUp).Row
If a = 2 Then a = 3
Range("a3:c" & a).ClearContents
searchcell = [b1]
counter = 0
'============================================================
'personal profile

ch.Start

ch.Get "https://www.amazon.eg/-/en/"

'============================================================
'search box
If Not ch.IsElementPresent(FindBy.ID("twotabsearchtextbox"), 3000) Then
'ch.Quit
MsgBox "Could not find search input box", vbExclamation
'Exit Sub
End If
ch.FindElementById("twotabsearchtextbox").SendKeys searchcell
'============================================================
'click search button
'if there is no search button ,press enter key
If Not ch.IsElementPresent(FindBy.ID("nav-search-submit-button")) Then
'ch.Quit
'MsgBox "Could not find submit button", vbExclamation
'Exit Sub
ch.FindElementById("twotabsearchtextbox", 3000).SendKeys (keys.Enter)
Else
ch.FindElementById("nav-search-submit-button", 3000).Click
End If

'============================================================
'loop all product items
Set items = ch.FindElementsByClass("s-card-container")

If items.Count = 0 Then
    ch.Quit
    MsgBox "Nothing at all was found", vbExclamation
    Exit Sub
End If
i = [a10000].End(xlUp).Row
If i = 2 Then i = 3
For Each item In items
'bb = item.FindElementByClass("a-size-base-plus").Text
'ff = bb.Count
 On Error Resume Next
If item.FindElementByClass("a-size-base-plus").Text <> "" Then
ff = item.FindElementByClass("a-size-base-plus").Text
Else
ff = item.FindElementByClass("a-size-medium-plus").Text
End If
        gg = item.FindElementByClass("a-link-normal").Attribute("href")
        Cells(i, 1) = ff
        Cells(i, 2) = item.FindElementByClass("a-price-whole").Text
        Cells(i, 3).Select
        ActiveCell.Hyperlinks.Add Anchor:=ActiveCell, Address:=gg, SubAddress:="", ScreenTip:="", TextToDisplay:="Goto Link"
        i = i + 1
Next item

'============================================================
'loop through all pages
'Set pager = ch.FindElementByXPath("///*[@id='search']/div[1]/div[1]/div/span[3]/div[2]/div[51]/div/div/span/a[3]")
' pager.Click

gg = ""

Application.ScreenUpdating = True
Application.EnableEvents = True

End Sub
`

==================================================================================

please help me
Vba and Selenium to scrap Amazon products.zip