遵循标准方式但未能使用VBA单击网页中的对象

时间:2018-05-07 10:13:43

标签: vba button web click

任何人都知道为什么网络没有回应

ClassName: ("J_SearchIpt search-btn iconfont-sf icon-sousuo")

OR

TagName: ("button"). 

非常感谢你!

单击按钮后,会弹出一个新选项卡。如果我想引用新标签,我的代码也是正确的。

谢谢!

Sub taobao()

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("完成!")

End Sub

1 个答案:

答案 0 :(得分:1)

请尝试以下按钮。

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

如果你没有得到你期望的结果,我会让windows处理一个新问题。