vba - 遵循标准方式,但无法使用 VBA 单击网页中的对象
问题描述
谁知道为什么网上没有回应
ClassName: ("J_SearchIpt search-btn iconfont-sf icon-sousuo")
或者
TagName: ("button").
太感谢了!
单击按钮后,会弹出一个新选项卡。如果我想参考新标签,我的代码是否也正确。
谢谢!
子淘宝()
Application.ScreenUpdating = False Application.Calculation = xlCalculationManual
Dim i As Integer
Dim x As Integer
Dim k As Integer
Dim pricehq As String
Dim price As String
x = InputBox("initial:")
k = InputBox("final:")
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
On Error Resume Next
For i = x To k
Dim properties As String
properties = Cells(i, 1).Value
'MsgBox properties
IE.navigate "https://sf.taobao.com/?spm=a213w.7398504.sfhead2014.2.1vQXr0¤t=index"
Application.Wait (Now + TimeValue("0:00:07"))
Dim Doc As HTMLDocument
Set Doc = IE.document
Set ptyinput = IE.document.getElementById("J_SearchTxt")
ptyinput.Value = properties
Application.Wait (Now + TimeValue("0:00:02"))
'Set ptyclick = IE.document.getElementByClassName("J_SearchIpt search-btn iconfont-sf icon-sousuo")
Set ptyclick = IE.document.getElementsByTagName("button")
ptyclick.Click
Application.Wait (Now + TimeValue("0:00:03"))
Dim objshell As Object
Set objshell = CreateObject("Shell.Application")
Set IE = objshell.Windows(1)
Application.Wait (Now + TimeValue("0:00:03"))
price = Trim(Doc.getElementsByClassName("price")(0).innerText)
If IsNumeric(Right(price, 1)) = True Then
Cells(i, 2).Value = price
Else
Cells(i, 2).Value = Trim(Doc.getElementById("price_hq").innerText)
price = ""
pricehq = ""
End If
Next i
Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic
MsgBox(“完成!”)
结束子
解决方案
尝试以下按钮。
Dim a As HTMLButtonElement
Set a = Doc.querySelector("button[class=""J_SearchIpt search-btn iconfont-sf icon-sousuo""]")
a.Click
如果你没有得到你期望的结果,我会让 windows 句柄成为一个新问题。
推荐阅读
- c# - 我可以在没有调用类的类对象的情况下订阅侦听器吗?
- bluetooth - 我想更改耳机按钮的功能。我想知道在哪里以及如何找到耳机的源代码/固件
- java - Tomcat(没有热部署)可以忽略覆盖的jar吗
- java - 'scomp' 不是内部或外部命令、可运行程序或批处理文件
- php - 在PHP中获取调用网页的域地址
- javascript - 访问匹配的子路由自定义道具(React Router v5)
- jquery - 将 div 文本添加到数组
- ruby-on-rails - 提交后禁用功能规范检查表单按钮的问题
- amadeus - 无法在 Amadeus Flight Offers API 中使用 includedCheckedBags 属性
- ruby-on-rails - RVM - Ruby 更新 - 无法自动检测当前活动的 RVM gem 集名称