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 和图片上传出现问题 的相关文章

  • 使用VBA复制垂直列并沿对角线粘贴

    我有一列数据 我们称之为 A 列 其中有 35 行数据 如何在此列上循环 然后将每个数据点粘贴到另一张工作表中 同时为每个循环循环增加列和行 换句话说 我寻求对角粘贴在第二张纸中 有没有一种简单的方法可以在 VBA 中执行此类操作 不要循环
  • VBA 激活 Internet Explorer 窗口

    我正在制作一个宏 用于打开 Internet Explorer 导航并登录网站 一切正常 但我需要将 IE 窗口放在前面并激活它 这样我就可以使用SendKeys在上面 我发现网站和视频在名为的命令上有不同的方法AppActivate我已经
  • VBA - HTML 抓取问题

    我正在尝试从网站上抓取拍卖数据https www rbauction com heavy equipment auctions https www rbauction com heavy equipment auctions 我当前的尝试是
  • JQuery更新数据-属性

    考虑以下相册示例 相册的首页和最后一页是相册跨页大小的一半 我设置了attr data width and attr data width correct 用于比较 例如 checkImageDimesions 附加对象数据 不是简单的字符
  • 在 MS Outlook 中,报告所有未收到回复的已发送邮件

    我每天都会发送大量电子邮件 但常常无法跟踪哪些邮件得到了实际回复 有没有办法使用 VBA 脚本查看上周发送的所有消息 并检查他们是否收到回复 具体来说 是一份已发送电子邮件的报告 这些电子邮件尚未从至少一个发送到的地址收到回复 我了解一点
  • 文本到行 VBA Excel

    我有一个电子表格 其中包含大约 4000 行数据 其中一列数据具有唯一的订单号 我希望使用 作为分隔符将其分隔 所以本质上我想要 Name Order Date Jane 123 001 111 08 15 2013 Gary 333 12
  • 在合并的单元格中选择、插入照片并将其居中

    我是一名研发面包师 正在为我的团队制作食谱模板 模板中有照片 但我需要轻松地允许他们单击一个按钮 打开照片的文件选择器 然后将该照片放在合并的单元格中 我其实不太擅长做这个 Sub InsertPhotoMacro Dim photoNam
  • 使用其innerHTML查找元素

    请看一下这个 DOM 树 div div span Home1 span div span Home2 span span Home3 span div 现在假设我有一个场景 我以某种方式获得了第一个跨度的innerHTMLHome1 是否
  • JasperReports 中每个工作表属性一页

    我有一个要求 我必须在 JasperReports 中设计一个报告 该报告有 4 页 第一张纸有 5 页 类似的其他纸有一页或两页 我面临的问题是 如果我使用net sf jasperreports export xls one page
  • 使用ASP/VB获取节点属性值

    我有以下 XML 架构
  • 我可以获取VBA代码中的注释文本吗

    可以说我有以下内容 Public Sub Information TEST End Sub 有没有办法得到 TEST 结果 不知何故通过VBA 例如 在 PHP 中 有一个获取注释的好方法 这里有什么想法吗 编辑 应该有办法 因为像 MZ
  • VBA 中 AND 函数如何工作?

    如果这是一个愚蠢的问题 我很抱歉 但是 Excel VBA AND 函数是否检查其中的每个条件然后继续 或者在第一个 FALSE 条件处停止而不检查其他条件 我想知道出于优化目的 但到目前为止在网上没有找到任何相关信息 提前致谢 示例 如果
  • VBA 中的匈牙利语好吗?

    我在 Net 中不使用匈牙利语 str int 前缀 但我仍然发现它在 VBA 中很有用 因为在 VBA 中很难看到类型 这很糟糕吗 不必要 也许我错过了一些东西 我真的很感激任何反馈 我想知道有一段时间了 谢谢大家 我想说 这种匈牙利符号
  • 如何等到 Excel 计算公式后再继续 win32com

    我有一个 win32com Python 脚本 它将多个 Excel 文件合并到电子表格中并将其另存为 PDF 现在的工作原理是输出几乎都是 NAME 因为文件是在计算 Excel 文件内容之前输出的 这可能需要一分钟 如何强制工作簿计算值
  • VBA ByRef 参数类型不匹配

    最初在我的主代码部分中 我有一个丑陋的 if 语句 尽管它会运行丑陋 我决定将其设为我要调用的函数 这导致我收到错误 编译错误 ByRef 参数类型不匹配 我的假设是该函数需要正确引用 尽管我一直在阅读文档并且不明白为什么 gt 声明 Sh
  • 在Excel中,我可以使用超链接来运行vba宏吗?

    我有一个包含多行数据的电子表格 我希望能够单击一个单元格 该单元格将使用该行中的数据运行宏 由于行数总是在变化 我认为每行的超链接可能是最好的方法 ROW MeterID Lat Long ReadX ReadY ReadZ CoeffA
  • 两个日期之间的小时数在 Excel 中不起作用

    根据要求 我提供了一张简化的屏幕截图来说明该问题 如您所见 我减去了两个日期并将其格式化为 h mm ss 为什么这不能提供两个日期之间经过的总小时数 有一个更好的方法吗 下面有一个很好的答案 但我试图弄清楚为什么按照此屏幕截图中所示的方式
  • 标志状态的 VBA 替换

    根据文档 Outlook 中的 MailItem FlagStatus 属性是已弃用 https msdn microsoft com en us library microsoft office interop outlook maili
  • 我如何以更好的方式编码而不是像这样的VBA编码

    我正在 Excel 中创建一个仪表板 但是我想知道是否有比这更好的编码方式 我想对其进行模块化 而不是这样做以使其更加整洁 Private Sub Afford If af Value True Then af afr Value Shee
  • 检查未绑定控件是否具有值的正确方法

    简单场景 一个表单和一个文本框 未绑定 Text1 If lt gt Text1 Then MsgBox Not Empty End If 上面的代码有效 表达方式 lt gt Text1如果文本框包含字符 则计算结果为 True 无论文本

随机推荐