Следовал стандартному способу, но не смог щелкнуть объект на веб-странице с помощью VBA - PullRequest
0 голосов
/ 07 мая 2018

Кто-нибудь знает, почему в Интернете нет ответа для

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

ИЛИ

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

End Sub

1 Ответ

0 голосов
/ 07 мая 2018

Попробуйте следующее для кнопки.

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

Я бы сделал вопрос о ручках окон новым вопросом, если вы не получите ожидаемого результата.

...