浏览网站 [英] Navigating through websites

查看:237
本文介绍了浏览网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码能够与它提出的第一个网页进行交互,但是一旦我离开它,我的代码就会给我一个'424'对象所需的错误。这是一个例子。

My code is able to interact with the first webpage it brings up, but then as soon as i navigate away from it, my code gives me a '424' object required error. Here is an example.

EX:转到yahoo.com - 搜索地球 - 点击雅虎徽标返回雅虎主页

EX: go to yahoo.com - search for 'Earth' - click on the Yahoo logo to return to the yahoo homepage

Sub InternetPractice()

Dim ie As InternetExplorer

Set ie = New InternetExplorer

ie.Visible = True

ie.navigate "https://www.yahoo.com"

Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop

ie.document.getElementById("uh-search-box").Value = "Earth"
ie.document.getElementById("uh-search-button").Click


Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop

ie.document.getElementById("logo").Click

End Sub

在互联网上使用VBA对我来说是新的,任何帮助都表示赞赏!

Using VBA with the internet is new to me and any help is appreciated!

推荐答案

编辑:



我认为你没有足够的时间来装载...它对我有用。
因此,您可以尝试使用此代码,以便在中止前加载3次。

I think you are not giving enough time for ie to load... It is working for me. So you can try this code that gives more time and try to load 3 times before aborting.

Option Explicit
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub InternetPractice2()


    Dim IE As InternetExplorer
    Dim HTMLDoc As HTMLDocument
    Dim logo As Object
    Dim n As Long
    Set IE = New InternetExplorer                'Set IE = CreateObject("InternetExplorer.Application")

    With IE
        .Silent = True
        .Visible = True
        .Navigate "https://www.yahoo.com"
    End With

    WaitIE IE, 5000

    Set HTMLDoc = IE.Document
    HTMLDoc.getElementById("uh-search-box").Value = "Earth"
    HTMLDoc.getElementById("uh-search-button").Click

load:
    WaitIE IE, 10000
    Set logo = HTMLDoc.getElementById("logo")
    If Not logo Is Nothing Then
        logo.Click
    ElseIf logo Is Nothing And n < 4 Then
        n = n + 1
        GoTo load
    End If
    WaitIE IE, 5000

    'Set ie to Nothing
    IE.Quit
    Set IE = Nothing
End Sub
Sub WaitIE(IE As Object, Optional time As Long = 250)
'Code from: https://stackoverflow.com/questions/33808000/run-time-error-91-object-variable-or-with-block-variable-not-set
Dim i As Long
Do
    Sleep time
    Debug.Print CStr(i) & vbTab & "Ready: " & CStr(IE.READYSTATE = 4) & _
                vbCrLf & vbTab & "Busy: " & CStr(IE.Busy)
    i = i + 1
Loop Until IE.READYSTATE = 4 Or Not IE.Busy
End Sub



原始答案



尝试关闭,即在代码末尾添加一些延迟,因为如果你步骤使用F8的代码,你会看到它的工作原理:

Original Answer

Try closing ie in the end of the code and add some delay, because if you step the code with F8, you will see that it works:

Sub InternetPractice2()

Dim ie As InternetExplorer

Set ie = New InternetExplorer

ie.Visible = True

ie.navigate "https://www.yahoo.com"

Do While ie.Busy = True Or ie.READYSTATE <> 4: DoEvents: Loop

ie.Document.getElementById("uh-search-box").Value = "Earth"
ie.Document.getElementById("uh-search-button").Click
'Add delay After .Click

Do While ie.Busy = True Or ie.READYSTATE <> 4: DoEvents: Loop
Application.wait Now+Timevalue("00:00:05")

ie.Document.getElementById("logo").Click
'Set ie to Nothing
Set ie = Nothing
End Sub

同时关闭所有iexplorer Windows任务管理器上的.exe。

Also close all iexplorer.exe on Windows Task Manager.

这篇关于浏览网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆