我正在嘗試為一個動態加載的網站進行網頁抓取,我今天嘗試了所有方法來關閉彈出圖示或單擊“不,謝謝”,但它不起作用,我在螢屏截圖中顯示錯誤。你能幫我在哪里錯嗎?
我需要在 excel 中輸入產品名稱、新舊價格以及它們的 href 影像 url。
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
uj5u.com熱心網友回復:
您嘗試抓取的網站似乎會根據您所在的位置提供不同的彈出視窗。
如果從阿聯酋查看,以下代碼會關閉“查看您的本地站點”彈出視窗和“加入我們的郵件串列”彈出視窗。
在嘗試與之互動之前,您需要給 WebDriver 時間來呈現檔案。timeout函式上的引數有助于FindElement*實作這一點,讓它們有時間可用。
這仍然不能保證找到了元素,因此Nothing在嘗試單擊之前進行測驗以查看結果是否是一個好主意。
我不認為你需要Selenium.By你可以使用的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
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/516086.html
