データを掻き集めるためにセレンクロムウェブドライバを使用してWebリンクを開くVBAを作成しましたが、私はあなたのアドバイスが必要です。VBAセレニウムFindElementByXPath doesntが要素を見つける
コード例と結果1:エラーの
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?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をactivedコード例と成果3:エラーでは
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
最初の例は離れて価格からではなく、時間の約2分の非常に長い期間にウェブサイトからの74の項目のすべてを返すを活性化しました。
2番目の例では、タイトルがシートの最初のセルにのみ返され、エラーがポップアウトされます。
3番目の例では21しか返されませんが、今はラベルを付けていない商品の価格を返すことはありません。スクリプトは10秒以下で非常に迅速に実行されます。
タイトル、サイズ、価格、hrefとともに74個のアイテムをすべて返す方法についてアドバイスをしてください。
正確なエラーは何ですか? StaleElement? –
エラースナップショットが2番目の例に付いているので、その意味がわかりません。第1および第3の例では、エラーを返すことはありません。 – Martin
ありがとう。私はVBで作業していないが、これは私がjavaのstalenessを克服するために使用したアプローチです。 https://stackoverflow.com/questions/45434381/stale-object-reference-while-navigation-using-selenium/45435158#45435158 –