You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VBA控制IE操作Google评论:解决评论页面滚动焦点异常问题

Fix: Focus on New Google Reviews Window Before Scrolling in VBA IE Automation

It sounds like your code is successfully opening the reviews page, but it's still targeting the original search window when trying to scroll—super common issue with IE's new window handling! Here's how to fix it by properly capturing the new reviews window, bringing it to focus, then executing your scroll:

Step-by-Step Solution

1. Capture the New Reviews Window Instance

When you click the "Google reviews" link, it spawns a new IE window. We need to grab that specific window object instead of continuing to use the original one. We can do this by enumerating all open IE windows and matching by URL or title (the reviews page URL will contain something like google.com/maps/reviews).

2. Activate the Window to Bring It to Focus

Once we have the new window object, we'll make it visible (just in case) and activate it to shift focus from the background search window.

3. Execute the Scroll on the Correct Window

Finally, run your scroll command on the new window's document object instead of the original.

Example Modified Code

Here's how to integrate this into your existing script:

Sub GoogleReviewsScroll()
    Dim IE As Object
    Dim IEReviews As Object
    Dim shellWindows As Object
    Dim window As Object
    Dim reviewsURL As String
    
    ' Initialize original IE window
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    IE.navigate "https://www.google.com/"
    
    ' Wait for page to load
    Do While IE.Busy Or IE.readyState <> 4
        DoEvents
    Loop
    
    ' Search for Anchor Bar, Buffalo NY
    IE.Document.getElementById("APjFqb").Value = "Anchor Bar, Buffalo NY"
    IE.Document.getElementById("btnK").Click
    
    ' Wait for search results to load
    Do While IE.Busy Or IE.readyState <> 4
        DoEvents
    Loop
    
    ' Click "Google reviews" link (adjust selector if needed)
    IE.Document.querySelector('a[href*="google.com/maps/reviews"]').Click
    
    ' Wait a moment for new window to spawn
    Application.Wait Now + TimeValue("00:00:02")
    
    ' Enumerate all IE windows to find the reviews page
    Set shellWindows = CreateObject("Shell.Application").Windows
    For Each window In shellWindows
        ' Check if the window is an IE instance and matches reviews URL pattern
        If InStr(window.FullName, "iexplore.exe") > 0 And InStr(window.LocationURL, "google.com/maps/reviews") > 0 Then
            Set IEReviews = window
            Exit For
        End If
    Next window
    
    ' Ensure we found the reviews window
    If Not IEReviews Is Nothing Then
        ' Activate the window to bring it to foreground
        IEReviews.Visible = True
        IEReviews.Activate
        
        ' Wait for reviews page to fully load
        Do While IEReviews.Busy Or IEReviews.readyState <> 4
            DoEvents
        Loop
        
        ' Execute single scroll operation (adjust pixel value as needed)
        IEReviews.Document.parentWindow.scrollBy 0, 600 ' Scroll down 600 pixels
        ' Alternative: IEReviews.Document.body.scrollTop = 600
    Else
        MsgBox "Could not locate the Google Reviews window!"
    End If
    
    ' Cleanup (optional)
    ' Set IE = Nothing
    ' Set IEReviews = Nothing
End Sub

Key Notes

  • Wait Timing: The Application.Wait after clicking the link gives time for the new window to initialize—adjust the duration if needed based on your internet speed.
  • Selector Adjustment: If the querySelector for the reviews link doesn't work, inspect the element to find a more reliable selector (like a specific class name or ID).
  • Window Matching: We use LocationURL to identify the reviews window, but you could also use document.title if the URL pattern isn't consistent.

This should ensure the scroll action targets the visible, focused reviews window instead of the background search page.

内容的提问来源于stack exchange,提问作者D4T4M4N

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:25:53