0

enter image description here

I'm trying to do the web scraping 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.

I need to fetch 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
2
  • Elements with those class names do not appear to exist on that page? (loading the given url redirects to the home page) Commented Oct 16, 2022 at 18:06
  • @NickSlash i have started learning coding recently, and i got this project. I have seen many videos and try to get the solution both in Python and VBA. Yes, though i have mentioned a different address it behaves like a home page, but if u manually closing the popup, it is taking me to the same page. Commented Oct 16, 2022 at 20:11

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

It worked perfectly this time script took me to products pages. Now I will try to get the detials of product using the similar method, thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.