首页 > 解决方案 > 我想单击 VBA 中的按钮

问题描述

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

我想使用 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

我想单击“新建项目”按钮以进一步移动

标签: htmlexcelvbaweb-scraping

解决方案


尝试使用 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 和尝试单击按钮之间进行适当的等待,然后在单击按钮后再次等待。


推荐阅读