2017-08-07 37 views
1

データを掻き集めるためにセレンクロムウェブドライバを使用して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 

enter image description hereを失活エラーオン

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 

enter image description here

コード例と結果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 

enter image description here

最初の例は離れて価格からではなく、時間の約2分の非常に長い期間にウェブサイトからの74の項目のすべてを返すを活性化しました。

2番目の例では、タイトルがシートの最初のセルにのみ返され、エラーがポップアウトされます。

3番目の例では21しか返されませんが、今はラベルを付けていない商品の価格を返すことはありません。スクリプトは10秒以下で非常に迅速に実行されます。

タイトル、サイズ、価格、hrefとともに74個のアイテムをすべて返す方法についてアドバイスをしてください。

+0

正確なエラーは何ですか? StaleElement? –

+0

エラースナップショットが2番目の例に付いているので、その意味がわかりません。第1および第3の例では、エラーを返すことはありません。 – Martin

+1

ありがとう。私はVBで作業していないが、これは私がjavaのstalenessを克服するために使用したアプローチです。 https://stackoverflow.com/questions/45434381/stale-object-reference-while-navigation-using-selenium/45435158#45435158 –

答えて

1

扱っているページにレイローディング方式が適用されています。これは、すべてのアイテムが同時にロードされないためです。むしろ、あなたが一番下にスクロールするとき、それは残りを読み込みます。私はコード内で小さなjavascript関数を使用し、問題を解決します。私はこれがあなたが探していた結果であることを願っています。

Sub test_supplements_store() 
    Dim driver As New ChromeDriver 
    Dim post As Object 

    driver.Get "https://www.thesupplementstore.co.uk/brands/optimum_nutrition" 
    On Error Resume Next 

    Do While EndofPage = False 
     PrevPageHeight = CurrentPageHeight 
     CurrentPageHeight = driver.ExecuteScript("window.scrollTo(0, document.body.scrollHeight);var CurrentPageHeight=document.body.scrollHeight;return CurrentPageHeight;") 
     driver.Wait 3000 
     If PrevPageHeight = CurrentPageHeight Then 
      EndofPage = True 
     End If 
    Loop 

    For Each post In driver.FindElementsByXPath("//li[contains(@class,'prod')]") 
     i = i + 1: Cells(i, 1) = post.FindElementByXPath(".//a").Attribute("title") 
     Cells(i, 2) = Split(post.FindElementByXPath(".//p[@class='size']").Text, ": ")(1) 
     Cells(i, 3) = post.FindElementByXPath(".//p[@class='price']//span[@class='now']//span|.//p[@class='price']//span[@class='dynamictype-single']").Text 
     Cells(i, 4) = post.FindElementByXPath(".//a").Attribute("href") 
    Next post 
End Sub 
+0

私に気づかなかった別の要件があります。 xpathを使うと価格の問題を解決できます。 – SIM

+0

あなたのコードは、ページの最初の21項目の価格のみを返します。また、私はどのようにアイテムの定期的かつ新しい価格を一緒に返すか分からない。 – Martin

+0

私はあなたの価格の部分を調整していませんでした。私は74項目すべてを取得しようとしました。 – SIM

関連する問題