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

2024-03-22

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

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

首先,当您用鼠标选择颜色时,会出现“上传照片”框。我无法使用 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")

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

如果我可以让图片上传工作,我就可以成功完成自动订单。这是我通过单击“添加到购物车”按钮获得的代码。我的整个“上传图片”部分不起作用,“选择颜色”下的最后一行不会显示“上传照片”框。

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\0 - 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

在我最初的帖子中,我解释了如何触发事件。现在我也解释如何单击“上传照片”按钮并聚焦弹出窗口。

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

这就是我现在所拥有的:

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

DOM VBA IE11 自动在网站上下订单 - OnChange 和图片上传出现问题 的相关文章