2016-09-07 24 views
3

按照this post我已經修復了對象檢查器。有時代碼可以正常運行10個條目,使它們全部正確,有時它會運行五次。有時會得到錯誤的條目。什麼是間歇性地打破我的短塊VBA?

它總是傾向於失敗的元素獲取innertertext。當Y/N結果錯誤時,我根本不知道是什麼原因造成的。

請幫忙!這讓我很生氣。我錯誤地檢查了每個階段。

Sub LetsAutomateIE() 

Dim barcode As String 
Dim rowe As Integer 
Dim document As HTMLDocument 
Dim Element As HTMLDivElement 
Dim text As String 
Dim pos As Integer 

Set ie = CreateObject("InternetExplorer.Application") 
rowe = 2 

While Not IsEmpty(Cells(rowe, 2)) 
    barcode = Cells(rowe, "B").Value 
    pos = 0 
    text = "" 
    Set document = Nothing 

    With ie 
     .Visible = False 
     .navigate2 "https://www.amazon.co.uk/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords=" & barcode 
     Do Until ie.readyState = 4 
     Loop 
    End With 

    Set document = ie.document 

    If IsObject(document.getElementById("result_0")) = False Then GoTo Here 

    text = document.getElementById("result_0").innerText 
    If InStr(text, "STEELBOOK") Or InStr(text, "Steelbook") Or InStr(text, "Steel book") <> 0 Then pos = 1 

    If pos <> 0 Then Cells(rowe, 4) = "Y" Else Cells(rowe, 4) = "N" 

    Here: 

    rowe = rowe + 1 
Wend 
Set ie = Nothing 

End Sub 

下面是我正在使用的示例條碼選擇。我從來沒有設法成功通過這些。

5030305517076 
5030305517816 
5060223767925 
5060223767949 
5060223767956 
5060223767970 
5060223767994 
8717418358563 
8717418365851 

太感謝你了,

山姆

+2

將'DoEvents'放入等待循環中嗎? – GSerg

回答

3

一個問題是,對於一些條形碼沒有結果發現這一事實。 如果你將與IE.Visible = true測試你的代碼,那麼你將看到的文字是這樣的:

您搜索「5060223767949」沒有匹配的產品。

另一個問題是條件IsObject(document.getElementById("result_0")) = False。這不太好,因爲IsObject(Nothing)返回true。最好是使用If <variable-name> Is Nothing Then ...

完整的代碼。 HTH

' Add reference to Microsoft Internet Controls (SHDocVw) 
' Add reference to Microsoft HTML Object Library 

Sub LetsAutomateIE() 
    Dim IE As SHDocVw.InternetExplorer 
    Dim barcode As String 
    Dim rowe As Integer 
    Dim document As HTMLDocument 
    Dim Element As HTMLDivElement 
    Dim result01 As HTMLListElement 
    Dim noResults As HTMLHeaderElement 
    Dim text As String 
    Dim pos As Integer 
    Dim url As String 

    rowe = 2 
    url = "https://www.amazon.co.uk/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords=" 
    Set IE = New SHDocVw.InternetExplorer 

    While Not IsEmpty(Cells(rowe, 2)) 
     barcode = Cells(rowe, "B").Value 
     pos = 0 
     text = "" 

     IE.Navigate url & barcode 

     While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE 
      DoEvents 
     Wend 

     Set document = IE.document 
     Set result01 = document.getElementById("result_0") 

     If result01 Is Nothing Then 
      Set noResults = document.getElementById("noResultsTitle") 
      If Not noResults Is Nothing Then MsgBox noResults.outerText 
      GoTo Here 
     End If 

     text = document.getElementById("result_0").innerText 
     If InStr(text, "STEELBOOK") Or InStr(text, "Steelbook") Or InStr(text, "Steel book") <> 0 Then pos = 1 

     If pos <> 0 Then Cells(rowe, 4) = "Y" Else Cells(rowe, 4) = "N" 

Here: 
     rowe = rowe + 1 
    Wend 

    IE.Quit 
    Set IE = Nothing 

End Sub 

實際上,我看只是爲了檢查的第一個返回 產品頁面上的標題...

的標題內li顯示與h2元素編號爲result_0。因此,可以將搜索僅限於此li元素,並搜索第一個元素h2

' text = document.getElementById("result_0").innerText 

Dim h2Elements As IHTMLElementCollection 
Dim h2 As HTMLHeadElement 

Set h2Elements = result01.getElementsByTagName("h2") 

If h2Elements.Length > 0 Then 
    Set h2 = h2Elements.Item(0) 
    text = h2.innerText 
    Debug.Print text 
Else 
    MsgBox "Text not found"  
End If 

輸出:

RED 2 Blu-ray Steelbook UK Exclusive 
The Hunger Games With Mockingjay Pendant 
The Hunger Games 
The Hunger Games 
Avengers Assemble BD Steelbook 
Avengers Assemble Bonus Disc BD Retail 
+0

非常感謝您的幫助!我看過沒有找到該產品,這就是爲什麼我試圖搜索results_0是否存在,因爲該框架在這種情況下不出現(我相信)。 我期待着嘗試你的代碼吧!再次感謝:) – Sam

+0

最後一個問題,如果有人絆倒在這可以幫助:我實際上只是看看頁面上第一個返回的產品的標題。我目前正在搜索frame/class中的所有文本/我不知道該怎麼稱呼它。是否有可能限制搜索只是標題? – Sam

+1

@Sam見編輯答案。我很高興它幫助:)。 – dee

0

我曾與document.getElementById("result_0")拋出一個錯誤的問題。我的解決方法是測試元素是否在Document.Body.InnerHTML中。

如果您將DebugMode設置爲True,則結果不良的網頁將保持打開狀態以供進一步檢查。

條形碼將被標記爲NA,如果未找到。

Option Explicit 

Sub LetsAutomateIE() 
    Const DebugMode As Boolean = True 
    Dim barcode As String, text As String 
    Dim rowe As Integer 
    Dim doc As HTMLDocument, liResults As HTMLLIElement 
    Dim ie As Object 

    Set ie = CreateObject("InternetExplorer.Application") 

    rowe = 2 

    While Not IsEmpty(Cells(rowe, 2)) 
     barcode = Cells(rowe, "B").Value 

     With ie 
      .Visible = False 
      .navigate2 "https://www.amazon.co.uk/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords=" & barcode 
      Do Until ie.readyState = 4 
      Loop 
     End With 

     Set doc = ie.document 
     If InStr(doc.body.innerHTML, "li id=""result_0""") Then 

      Set liResults = doc.getElementById("result_0") 
      text = liResults.innerText 
      Cells(rowe, 4) = IIf(InStr(text, "STEELBOOK") Or InStr(text, "Steelbook") Or InStr(text, "Steel book"), "Y", "N") 

     Else 
      Cells(rowe, 4) = "NA" 
      If DebugMode Then 
       ie.Visible = True 
       Set ie = CreateObject("InternetExplorer.Application") 
      End If 
     End If 
     rowe = rowe + 1 
    Wend 

    ie.Quit 

    Set ie = Nothing 

End Sub 
+0

非常感謝托馬斯:) – Sam

+1

不客氣山姆 – 2016-09-07 13:03:14

相關問題