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

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.

First, Fix the Most Common Culprit: Incomplete Page Loading

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
Double-Check Your Element Locators

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" or By.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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:34:54