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

戏子无情 提交于 2020-01-16 17:20:04

问题


We have a Virtual Assistant placing hundreds of orders for ball markers on this site: https://www.golfballs.com/Golf-Misc/Tools/Classic-Photo-Poker-Chips-3-Pack.htm

I had used VBA before to get data from a website but I would like to use it to automate the placing of orders. I can get close but there are a few things tripping me up.

First of all, when you select a color with your mouse, the "Upload a Photo" box appears. I cannot get the box to show up using my VBA code.

Using VBA, I cannot make the onchange event fire for the life of me. I have tried the following four combinations:

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")

Second, even if I can get the boxes to show and I click on "Upload a Photo", the popup box is there and I am having trouble putting the focus on it and I am unsure on how to tell the ID "fileField" what picture from my browse I want to upload. There is also a second Confirmation pop up.

If I can get the picture upload to work, I can successfully complete the automated order. Here is my code through clicking the Add to Cart button. The entirety of my "Upload Picture" section does not work and the last line under "Select the Color" does not make the "Upload a Photo" box show.

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

回答1:


In my original posting I explained how to trigger an event. Now I explain too how to click the "Upload a photo" button and focus the popup window.

The problem now is, the needed html document is in an iframe I can't access. I know there can be different reasons but none of them solve the problem.

This is, what I have now:

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

And this procedure to trigger the event you need:

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


来源:https://stackoverflow.com/questions/59113561/dom-vba-ie11-automate-placing-orders-on-a-website-trouble-with-onchange-and-pi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!