【问题标题】:I want to click the button in VBA我想单击 VBA 中的按钮
【发布时间】:2019-04-29 14:11:33
【问题描述】:

我是初学者和非技术人员,我完全卡在这里

我想使用 VBA 点击新建项目按钮

这是新项目按钮的检查元素 <button tabindex="0" class="xui-button xui-button-create xui-button-small" type="button" data-automationid="project-modal-new-button">New project</button>

下面是代码,到目前为止我已经创建,它运行没有任何问题,它将带我从项目选项卡进入项目选项卡我需要使用 VBA 单击新的项目按钮

Sub Xero()

Dim i As SHDocVw.InternetExplorer
Set i = New InternetExplorer
i.Visible = True
i.navigate ("https://login.xero.com/")
Do While i.readyState <> READYSTATE_COMPLETE
Loop
Dim idoc As MSHTML.HTMLDocument
Set idoc = i.document
idoc.all.UserName.Value = "XXXXX"
idoc.all.Password.Value = "123"

Dim ele As MSHTML.IHTMLElement
Dim eles As MSHTML.IHTMLElementCollection
Set eles = idoc.getElementsByTagName("button")

For Each ele In eles
    If ele.ID = "submitButton" Then
    ele.Click
    Application.Wait (Now + TimeValue("0:00:10"))
    Else
    End If
Next ele

i.navigate ("https://projects.xero.com/?CID=!QhJgj")

Do While i.readyState <> READYSTATE_COMPLETE
Loop

End Sub

我想点击新建项目按钮进一步移动

【问题讨论】:

    标签: html excel vba web-scraping


    【解决方案1】:

    尝试使用 css 类选择器(与 getElementsByClassName 相同,但更快)

    While i.Busy Or i.readyState < 4: DoEvents: Wend
    Application.Wait Now + Timeserial(0,0,2)
    i.document.querySelector(".xui-button-create").click
    

    另外,在.click.navigate 之后使用适当的页面等待,即

    While i.Busy Or i.readyState < 4: DoEvents: Wend
    

    因此,在 .navigate 和尝试单击按钮之间进行适当的等待,然后在单击按钮后再次等待。

    【讨论】:

    • 非常感谢您的快速响应,它正在运行但得到“运行时错误'424':需要对象',一旦我按下调试按钮,它就会打开新项目选项卡。
    • 遇到问题了吗?
    • 所以在Application.Wait Now + Timeserial(0,0,2)的点击行前加一个延迟
    猜你喜欢
    • 2019-01-11
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多