Selenium/Python:如何切换到新页面并定位页面元素
Hey there! Let's tackle this problem step by step. I've run into this exact issue with Google Search and Selenium before, so I know the common pitfalls here.
When you hit enter and the browser navigates to the results page, Google dynamically renders its search results—Selenium might try to locate elements before they’ve fully loaded into the DOM, leading to failures. The most reliable fix here is using explicit waits instead of arbitrary time.sleep() calls.
Here’s a working code example tailored to your Google Search scenario:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Initialize your browser (Chrome in this case) driver = webdriver.Chrome() driver.get("https://www.google.com") # Perform the search search_box = driver.find_element(By.NAME, "q") search_box.send_keys("Selenium Python web scraping") search_box.submit() # Wait up to 10 seconds for search result titles to appear wait = WebDriverWait(driver, 10) search_result_titles = wait.until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, "div.g h3")) ) # Now you can interact with the elements for title in search_result_titles: print(title.text)
While you mentioned the browser navigates directly to the results page, if for some reason the search opened a new window, you’ll need to switch window handles:
# Get all open window handles all_windows = driver.window_handles # Switch to the most recently opened window (last item in the list) driver.switch_to.window(all_windows[-1]) # Then use the explicit wait method above to locate elements
Google occasionally tweaks its page structure, so make sure your locator is accurate. Use your browser’s dev tools (F12) to inspect elements:
- Search result titles often work with
By.CSS_SELECTOR, "h3"orBy.XPATH, "//h3" - Result links can be targeted with
By.CSS_SELECTOR, "div.g a"
Also, don’t forget to handle any popups (like Google’s privacy consent prompt) before trying to locate search results—these can block element access.
内容的提问来源于stack exchange,提问作者Hannan

