首页 > 解决方案 > 遵循标准方式,但无法使用 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&current=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(“完成!”)

结束子

标签: vbabuttonwebclick

解决方案


尝试以下按钮。

Dim a As HTMLButtonElement
Set a = Doc.querySelector("button[class=""J_SearchIpt search-btn iconfont-sf icon-sousuo""]")
a.Click

如果你没有得到你期望的结果,我会让 windows 句柄成为一个新问题。


推荐阅读