我已經寫了一個VBA,它使用硒鉻驅動程序打開一個Web鏈接來抓取數據,我得到了幾個問題,我需要你們對你們的建議。VBA Selenium FindElementByXPath找不到元素
代碼示例和結果1: 在錯誤actived
Sub test_supplements_store()
Dim driver As New ChromeDriver
Dim post As Object
i = 1
driver.Get "https://www.thesupplementstore.co.uk/brands/optimum_nutrition?page=4"
On Error Resume Next
For Each post In driver.FindElementsByClass("desc")
Cells(i, 1) = post.FindElementByTag("a").Attribute("title")
Cells(i, 2) = Trim(Split(post.FindElementByClass("size").Text, ":")(1))
Cells(i, 3) = post.FindElementByXPath(".//span[@class='now']//span[@class='pricetype-purchase-unit multi-price']//span[@class='blu-price blu-price-initialised']").Text
Cells(i, 4) = post.FindElementByTag("a").Attribute("href")
i = i + 1
Next post
End Sub
代碼示例和結果2:在錯誤停用
Sub test_supplements_store()
Dim driver As New ChromeDriver
Dim post As Object
i = 1
driver.Get "https://www.thesupplementstore.co.uk/brands/optimum_nutrition?page=4"
'On Error Resume Next
For Each post In driver.FindElementsByClass("desc")
Cells(i, 1) = post.FindElementByTag("a").Attribute("title")
Cells(i, 2) = Trim(Split(post.FindElementByClass("size").Text, ":")(1))
Cells(i, 3) = post.FindElementByXPath(".//span[@class='now']//span[@class='pricetype-purchase-unit multi-price']//span[@class='blu-price blu-price-initialised']").Text
Cells(i, 4) = post.FindElementByTag("a").Attribute("href")
i = i + 1
Next post
End Sub
Sub test_supplements_store()
Dim driver As New ChromeDriver
Dim post As Object
i = 1
driver.Get "https://www.thesupplementstore.co.uk/brands/optimum_nutrition"
On Error Resume Next
For Each post In driver.FindElementsByClass("desc")
Cells(i, 1) = post.FindElementByTag("a").Attribute("title")
Cells(i, 2) = Trim(Split(post.FindElementByClass("size").Text, ":")(1))
Cells(i, 3) = post.FindElementByXPath(".//span[@class='now']//span[@class='pricetype-purchase-unit multi-price']//span[@class='blu-price blu-price-initialised']").Text
Cells(i, 4) = post.FindElementByTag("a").Attribute("href")
i = i + 1
Next post
End Sub
第一個例子返回所有從該網站74項除了價格,但在很長的時間大約兩分鐘時間。
第二個示例僅將標題返回到工作表的第一個單元格並彈出錯誤。
第三個示例僅返回21,但錯過了沒有現在標籤的商品的退貨價格。腳本運行速度非常快,不到10秒。
請諮詢如何將所有74個項目返回到標題,大小,價格,href。
你得到了什麼確切的錯誤? StaleElement? –
我不確定你是什麼意思,因爲錯誤快照附加到第二個例子。第一個和第三個示例不會返回任何錯誤。 – Martin
好的謝謝。我沒有在VB上工作,但這是我用來克服java中過時的方法。 https://stackoverflow.com/questions/45434381/stale-object-reference-while-navigation-using-selenium/45435158#45435158 –