简体   繁体   English

VBA按钮单击有时可以工作

[英]VBA button click working sometimes

I'm just getting started with VBA and I was following a simple tutorial to use VBA to make a Google Search in IE. 我刚刚开始使用VBA,我正在遵循一个简单的教程来使用VBA在IE中进行Google搜索。

Sub FillInternetForm()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://www.google.com"
IE.Visible = True
While IE.busy
DoEvents 'wait for page to finish loading
Wend
IE.Document.All("q").Value = ThisWorkbook.Sheets("sheet1").Range("a1")
IE.Document.All("btnK").Click
End Sub

I ran this code multiple times (by pressing f5) and it appears that IE.Document.All("btnK").Click only works sometimes. 我多次运行此代码(按f5键),看来IE.Document.All("btnK").Click有时只能工作。 Sometimes the Macros pop up will appear, sometimes not. 有时会出现“ 宏”弹出窗口 ,有时则不会。 I was just wondering what could be the cause of this or if there's anyway to prevent this from happening. 我只是想知道这可能是什么原因,或者是否有阻止这种情况发生的方法。

I am using Excel 2013 我正在使用Excel 2013

Thank you 谢谢

You could avoid the search button altogether and concatenate the search term into the URL direct. 您可以完全避免使用搜索按钮,并将搜索词直接连接到URL中。 For example: 例如:

Option Explicit
Sub FillInternetForm()
    Dim IE As Object, searchTerm As String
    searchTerm = ThisWorkbook.Worksheets("Sheet1").Range("A1")
    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .navigate "https://www.google.co.uk/search?safe=strict&source=hp&ei=XiooW8rAKMeZsgGji57gBw&q=" & searchTerm & "&oq=" & searchTerm & "&gs_l=psy-ab.3..0l2j0i131k1j0l2j0i131k1j0j0i131k1l3.1481.2118.0.2518.7.6.0.0.0.0.124.555.0j5.6.0..3..0...1.1.64.psy-ab..1.6.657.6..35i39k1.103.n22OrWdTQrs"
        .Visible = True
        While .Busy Or .readyState < 4: DoEvents: Wend
     Stop '<= Delete this line
    .Quit
     End With
End Sub

If you do an example search you can capture the URL and the edit in your variable name to string, as I show above. 如上所示,如果您进行示例搜索,则可以捕获URL并将变量名中的编辑捕获为字符串。

As @RyszardJędraszyk explains (many thanks), you may have a dynamically loaded webpage in which content is not available at the moment you try to grab hold of an element. 正如@RyszardJędraszyk解释的(非常感谢),您可能有一个动态加载的网页,在您尝试获取元素时,其中的内容不可用。 In this case you can have a loop which attempts to set to an element that you know should be present and loop while is nothing (with timeout). 在这种情况下,您可以有一个循环,该循环尝试将其设置为您知道应该存在的元素,然后循环为空(超时)。

Ryszard further points out you are missing your ReadyState = 4 fully loaded (which does mean a dynamic page, for example, is actually fully loaded, or that a lazy-loading page has all items visible (you may require scrolling to achieve this. My answer above shows you how to also use the ReadyState . Ryszard进一步指出,您缺少ReadyState = 4完全加载(例如,这意味着动态页面实际上已完全加载,或者惰性加载页面具有所有可见项目(您可能需要滚动才能实现此目的)。上面的答案向您展示了如何还使用ReadyState

I was unsure about the cause of this pop-up, so I just went with the simplest route to get the page of interest, to try and lessen the risk of this happening. 我不确定发生此弹出式窗口的原因,所以我只是采用了最简单的方法来获得感兴趣的页面,以尝试减少这种情况的发生。 It is not something I have ever had happen or could reproduce I'm afraid. 恐怕这不是我曾经发生或可能复制的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM