间歇性地打破了我的短块VBA的是什么?

根据这篇文章,我已经修复了对象检查器。 有时代码可以正常运行10个条目,使它们全部正确,有时会运行五个。 有时会得到错误的条目。

它总是趋于失败的元素的密码。 当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 

这是我正在使用的示例条形码的select。 我从来没有设法成功通过这些。

 5030305517076 5030305517816 5060223767925 5060223767949 5060223767956 5060223767970 5060223767994 8717418358563 8717418365851 

非常感谢,

山姆

一个问题是,对于一些条形码没有find结果。 如果你要用IE.Visible = true来testing你的代码,那么你会看到这样的文字:

您的search“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元素显示,结果为id result_0 。 所以可以将search限制在这个li元素上并search第一个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 

我有一个document.getElementById("result_0")抛出一个错误的问题。 我的解决方法是testing元素是否在Document.Body.InnerHTML中。

如果将DebugMode设置为True,那么结果不佳的网页将保持打开状态,以便进一步检查。

条码将被标记为NA,如果没有find。

 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