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

Python Selenium:如何循环刷新页面直至找到指定元素?

Hey there! Let's fix your issue with refreshing the page until the target element shows up and handling the BOM replacement. Here are two reliable approaches using Selenium with ChromeDriver:

Fix: Refresh Page Until Target Element is Found & Remove BOM

Basic Loop with Exception Handling

This straightforward approach uses a loop to repeatedly check for the element, refresh the page if it's missing, and stop once we find it (or hit a max attempt limit to avoid infinite loops):

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time

# Initialize ChromeDriver
driver = webdriver.Chrome()
driver.get("YOUR_TARGET_PAGE_URL")

# Set a maximum number of refresh attempts to prevent infinite loops
max_refresh_attempts = 20
current_attempt = 0

while current_attempt < max_refresh_attempts:
    try:
        # Locate the element by its class name
        target_element = driver.find_element_by_class_name("YOUR_TARGET_CLASS_NAME")
        
        # Remove BOM (common UTF-8 BOM is \ufeff; adjust if your BOM is different)
        cleaned_text = target_element.text.replace("\ufeff", "")
        print("Cleaned text without BOM:", cleaned_text)
        
        # Exit the loop once we successfully find and process the element
        break
    
    except NoSuchElementException:
        print(f"Attempt {current_attempt + 1}: Element not found, refreshing page...")
        driver.refresh()
        current_attempt += 1
        time.sleep(2)  # Wait 2 seconds after refresh to let the page load

# If we exit the loop without breaking, we hit the max attempts
else:
    print(f"Failed to find element after {max_refresh_attempts} refresh attempts.")

# Clean up the driver
driver.quit()

Elegant Approach with WebDriverWait

For a more Selenium-idiomatic solution, use WebDriverWait with a custom condition. This handles the polling and waiting logic for you, and we'll add automatic refreshes when the element is missing:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException, TimeoutException

driver = webdriver.Chrome()
driver.get("YOUR_TARGET_PAGE_URL")

def check_for_element_and_refresh(driver):
    """Custom condition: return the element if found, else refresh and return False"""
    try:
        return driver.find_element_by_class_name("YOUR_TARGET_CLASS_NAME")
    except NoSuchElementException:
        driver.refresh()
        return False

try:
    # Wait up to 30 seconds, check every 2 seconds
    target_element = WebDriverWait(driver, 30, poll_frequency=2).until(check_for_element_and_refresh)
    
    # Remove BOM from the element's text
    cleaned_text = target_element.text.replace("\ufeff", "")
    print("Cleaned text without BOM:", cleaned_text)

except TimeoutException:
    print("Timed out waiting for the element to appear after multiple refreshes.")

driver.quit()

Key Notes to Troubleshoot Further

  • Verify your class name: Double-check that the class name you're using is exactly correct (no typos, and it's the class of the element you want—find_element_by_class_name returns the first matching element).
  • Identify the correct BOM character: If \ufeff doesn't work, print the raw text representation to see hidden characters:
    print(repr(target_element.text))
    
    This will show you the exact BOM character (e.g., \ufffe for UTF-16 BOM) to replace.
  • Why find_element_by_partial_link_text failed: This method only works for <a> (link) elements. If your target element isn't a link, or its text is dynamically rendered/contains hidden characters, this method won't work—using class name is a solid alternative here.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:57:02