VBA控制IE操作Google评论:解决评论页面滚动焦点异常问题
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.Waitafter 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
querySelectorfor 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
LocationURLto identify the reviews window, but you could also usedocument.titleif 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

