我正在为我工作的公司开展一个项目,并且是VBA的新手。我正在尝试登录供应商的网站并导航到价格查询页面。
我能够登录但登录后我无法让VBA点击我想要的超链接,除非我使用F8单步执行代码。我试图在代码中的某些位置暂停,但它仍无法正常工作。
我发布了代码中出现问题的部分。任何帮助将不胜感激。
Sub getDurablePrices()
Dim IE As Object
Dim i As Long
Dim objElement As Object
Dim objCollection As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "http://duraweb.durableusa.com/Acct%20Mgmt/Login.aspx"
IE.Visible = True
Do While IE.Busy
'nothing
Loop
IE.document.All("htmUserName").Value = "******"
IE.document.All("htmPwd").Value = "******"
Set objCollection = IE.document.getElementsByTagName("input")
i = 0
While i < objCollection.Length
If objCollection(i).Type = "submit" Then
Set objElement = objCollection(i)
End If
i = i + 1
Wend
objElement.Click
'Code added as suggested.
Do While IE.Busy Or IE.readyState <> 4
DoEvents
Loop
Set AllHyperLinks = IE.document.getElementsByTagName("A")
For Each hyper_link In AllHyperLinks
If hyper_link.innerText = "Customer Price Inquiry" Then
hyper_link.Click
Exit For
End If
Next
End Sub
以下是我试图点击的HTML:
<a class="ctl00_ContentPlaceHolder1_MenuTree_0"
href="Customer/CustomerPriceInquiry.aspx"
id="ctl00_ContentPlaceHolder1_MenuTreet4">Customer Price Inquiry</a>
答案 0 :(得分:0)
不要使用计时器 - 而是等待页面完成加载,如下所示:
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
...或者作为我在任何“点击”或导致页面更改的其他操作后放置的单行:
Debug.Print"Waiting, ";:Do While ie.Busy Or ie.readyState<>4:DoEvents:Loop:Debug.Print"Loaded."
答案 1 :(得分:0)
你有没有尝试过第二次
Do While IE.Busy Or IE.readyState <> 4
DoEvents
Loop
然后再做
IE.navigate Trim$(Split(Split(IE.document.body.innerHTML, "id=""ctl00_ContentPlaceHolder1_MenuTreet4""")(0), "href =")(1))
这是假设你做不到
IE.document.getElementByID("ctl00_ContentPlaceHolder1_MenuTreet4").Click
或
Dim targetElement As Object
Do
On Error Resume Next
Set targetElement = IE.document.getElementById("ctl00_ContentPlaceHolder1_MenuTreet4")
On Error GoTo 0
DoEvents
Loop While targetElement Is Nothing