DOM VBA IE11 在网站上自动下订单 - OnChange 和图片上传有问题

DOM VBA IE11 Automate Placing Orders on a Website - Trouble With OnChange and Picture Upload

我们有一个虚拟助手在这个网站上下了数百个球标订单: https://www.golfballs.com/Golf-Misc/Tools/Classic-Photo-Poker-Chips-3-Pack.htm

我以前曾使用 VBA 从网站获取数据,但我想用它来自动下订单。我可以接近,但有一些事情让我绊倒了。

首先,当您用鼠标 select 一种颜色时,会出现 "Upload a Photo" 框。我无法使用我的 VBA 代码显示该框。

使用 VBA,我一辈子都无法触发 onchange 事件。我尝试了以下四种组合:

doc.getElementById("2").selectedIndex = 2
doc.getElementById("2").FireEvent ("onchange")

doc.getElementById("2").Focus
doc.getElementById("2").selectedIndex = 2
doc.getElementById("2").FireEvent ("onchange")

doc.getElementById("2").selectedIndex = 2
doc.getElementById("2").FireEvent ("onclick")

doc.getElementById("2").Focus
doc.getElementById("2").selectedIndex = 2
doc.getElementById("2").FireEvent ("onclick")

其次,即使我可以显示框并单击 "Upload a Photo",弹出框仍然存在,但我无法将焦点放在它上面,而且我不确定如何告诉ID "fileField" 我要上传我浏览的什么图片。还有第二个确认弹出窗口。

如果我可以让图片上传工作,我就可以成功完成自动订单。这是我通过单击“添加到购物车”按钮的代码。我的整个 "Upload Picture" 部分都不起作用,"Select the Color" 下的最后一行没有显示 "Upload a Photo" 框。

Dim IE As InternetExplorer
Dim doc As HTMLDocument

Set IE = New InternetExplorer
IE.Visible = True

'Go to the Ball Marker Page
ballMarkerURL = "https://www.golfballs.com/Golf-Misc/Tools/Classic-Photo-Poker-Chips-3-Pack.htm"
IE.navigate ballMarkerURL

'Wait for page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
Set doc = IE.document

'Select the Color
doc.getElementById("2").Focus
doc.getElementById("2").selectedIndex = 2
doc.getElementById("2").FireEvent ("onchange")

'Upload Picture
markerFilePath = "M:\Cuddle Clones Share (Team Folder)\Operations\Vendors\Pet Prints[=13=] - Ready to Order - Golfballs.com\"
markerFileName = "380844 - Ball Marker - 200604-Red-1-of-1-qty-1.png"
fullString = markerFilePath & markerFileName
doc.getElementById("copyright_check").Checked
doc.getElementById("fileField").Value = fullString
doc.getElementById("upload").Click
doc.getElementById("saveBtn").Click

'Update Quantity
doc.getElementById("formQty").Value = 2

'Add to Cart
doc.getElementsByClassName("buttonStatic r addCart")(0).Click

在我原来的帖子中,我解释了如何触发事件。现在我也解释了如何单击 "Upload a photo" 按钮并聚焦弹出窗口 window.

现在的问题是,所需的 html 文档位于我无法访问的 iframe 中。我知道可能有不同的原因,但其中 none 个可以解决问题。

这是我现在拥有的:

Sub SelectColorForGolfballs()

Dim objShell As Object
Dim objWindow As Object
Dim browser As Object
Dim url As String
Dim nodeColorDropDown As Object
Dim nodeThreeButtons As Object
Dim browserPopUp As Object
Dim nodeFrames As Object
Dim nodeIframeDoc As Object

  Set objShell = CreateObject("Shell.Application")
  url = "https://www.golfballs.com/Golf-Misc/Tools/Classic-Photo-Poker-Chips-3-Pack.htm"

  'Initialize Internet Explorer, set visibility,
  'call URL and wait until page is fully loaded
  Set browser = CreateObject("internetexplorer.application")
  browser.Visible = True
  browser.navigate url
  Do Until browser.ReadyState = 4: DoEvents: Loop

  'Select color from dropdown
  Set nodeColorDropDown = browser.document.getElementByID("2")
  nodeColorDropDown.selectedIndex = 6 'Pink for testing
  Call TriggerEvent(browser.document, nodeColorDropDown, "change")
  'Manual break for loading the page complitly
  'Application.Wait (Now + TimeSerial(pause_hours, pause_minutes, pause_seconds))
  Application.Wait (Now + TimeSerial(0, 0, 2))

  'Open Picture Upload
  'The document changed, so you can't work with the old document here
  'In a first step you need the div element with the three buttons
  'we get in the last step by trigger dropdown event
  '
  '&lt;div class="options-gallery"&gt;
  '  &lt;a href="javascript:productSelection(1, 'P');"&gt;
  '    &lt;img src="https://d1tp32r8b76g0z.cloudfront.net/images/property/button/Half/Condition_P.jpg" title="Personalized" border="0"&gt;
  '  &lt;/a&gt;
  '  &lt;a href="javascript:productSelection(1, 'S');"&gt;
  '    &lt;img src="https://d1tp32r8b76g0z.cloudfront.net/images/property/button/Half/Condition_S.jpg" title="Photo" border="0"&gt;
  '  &lt;/a&gt;
  '  &lt;a href="javascript:productSelection(1, 'L');"&gt;
  '    &lt;img src="https://d1tp32r8b76g0z.cloudfront.net/images/property/button/Half/Condition_L.jpg" title="Novelty" border="0"&gt;
  '  &lt;/a&gt;
  '&lt;/div&gt;
  Set nodeThreeButtons = browser.document.getElementsByClassName("options-gallery")(0)

  'The second button must be clicked
  nodeThreeButtons.FirstChild.NextSibling.Click
  Application.Wait (Now + TimeSerial(0, 0, 2))

  'Focus popup by runnig throuhg all open windows
  For Each objWindow In objShell.Windows
    'Check if it's an IE
    If InStr(1, UCase(objWindow.FullName), "IEXPLORE") > 0 Then
      'Check if it's the right IE
      If InStr(1, objWindow.document.getElementsByTagName("title")(0).innertext, "iCusomize Image Selection") Then
        Set browserPopUp = objWindow
        Exit For
      End If
    End If
  Next objWindow

  'Now we can work with the popup
  'It has only short code over all and a very short body
  'You have to access the content of an iFrame
  '
  'Problem: I don't know why the following don't work
  'I know it can't be in the same line
  'You must split the access to the iFrame
  'Get a node collection of all frames/ iframes of the document
  Set nodeFrames = browserPopUp.document.frames

  'The following line couses the error "Access denied"
  'Select the first (and only) frame from the node collection
  Set nodeIframeDoc = nodeFrames(0).document

  'Check the copyright checkbox
  nodeIframeDoc.getElementByID("copyright_check").Click

  'If you are at this point we can look ahead
End Sub

而这个程序触发你需要的事件:

Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)

  Dim theEvent As Object

  htmlElementWithEvent.Focus
  Set theEvent = htmlDocument.createEvent("HTMLEvents")
  theEvent.initEvent eventType, True, False
  htmlElementWithEvent.dispatchEvent theEvent
End Sub