I'm trying to do the webscrapping for a website which is dynamically loaded, I tried everything today to close the popup icon or clicking "No thanks", but it is not working and i am getting error showing in screenshot. Can you please help where i am wrong.
I need to fecth the product name, new and old price and their href image url in excel.
Option Explicit
Private cd As Selenium.ChromeDriver
Sub Findingelement()
Set cd = New Selenium.ChromeDriver
cd.Start
cd.Get "https://www.westelm.com/shop/furniture/all-living-room/?cm_type=gnav&originsc=furniture"
Dim dismiss As Selenium.WebElement
Dim findby As New Selenium.By
Dim closepopup As Selenium.WebElement
Set dismiss = cd.FindElement(findby.Class("dismiss-overlay-text"))
dismiss.Click
Set closepopup = cd.FindElement(findby.XPath("//div[@class='no-thanks']/a[@class='overlayCloseButton stickyHeaderCloseButton not-into-savings']"))
closepopup.Click
End Sub
CodePudding user response:
The website you are trying to scrape seems to serve different popups depending on where you are located.
The following code dismisses a "view your local site" popup, and a "join our mailing list" popup if viewed from UAE.
You need to give the WebDriver time to render the document before you try to interact with it. The timeout
parameter on the FindElement*
functions helps with this, allowing time for them to become available.
This is still not a guarantee that the elements were found, so testing to see if the result is Nothing
before attempting to click is a good idea.
I don't think you need the Selenium.By
as you can use WebDriver.FindElementByXPath
Public Driver As Selenium.ChromeDriver
Sub Main()
Set Driver = New Selenium.ChromeDriver
Dim Element As Selenium.WebElement
Driver.Get "https://www.westelm.com/shop/furniture/all-living-room/?cm_type=gnav&originsc=furniture"
Set Element = Driver.FindElementByClass("dismiss-overlay-text", 5000) ' wait max of 5000ms (5s) for element to be available
If Element Is Nothing Then
Debug.Print "failed: dismiss overlay"
Else
Debug.Print "success: dismissed overlay"
Element.Click
End If
Set Element = Driver.FindElementByXPath("//*[@id='join-email']/div/div[3]/div/a", 5000)
If Element Is Nothing Then
Debug.Print "failed: dismiss promotion"
Else
Debug.Print "success: dismissed promotion"
Element.Click
End If
End Sub