Selenium页面加载时动态加载文本循环异常求助
Fixing the Looping Loading Animation in Selenium Thread
Let's break down why your current code stops after one full cycle and stays stuck on Loading...:
- Thread termination timing: When
website_loaded = Trueis set, your loading thread is likely still in the finaltime.sleep(0.5)call after writingLoading.... Once the sleep finishes, the loop exits immediately, leaving the last printed state lingering on the console. - Unclean console output: Since each subsequent loading string is longer than the previous, when you loop back to
Loading., the extra dots fromLoading...aren't overwritten (the\ronly moves the cursor to the start of the line, it doesn't erase existing characters beyond the new string's length).
Here's the revised code that fixes both issues, plus uses a thread-safe Event instead of a global boolean for better reliability:
import threading import sys from selenium import webdriver import time # Use a thread-safe Event instead of a raw global boolean website_loaded = threading.Event() driver = webdriver.Firefox(executable_path=r"C:\Users\Username\Documents\Python\geckodriver.exe") # Fix: sys.argv[1:] returns a list — use sys.argv[1] for single URL, or join if handling multiple args website = sys.argv[1] if len(sys.argv) > 1 else "https://example.com" def loading(): while not website_loaded.is_set(): # Add trailing spaces to overwrite longer previous loading states sys.stdout.write("Loading. \r") sys.stdout.flush() # Wait with timeout to check if page loaded early if website_loaded.wait(0.5): break sys.stdout.write("Loading.. \r") sys.stdout.flush() if website_loaded.wait(0.5): break sys.stdout.write("Loading... \r") sys.stdout.flush() if website_loaded.wait(0.5): break # Clear the loading line and print completion message sys.stdout.write("Page loaded successfully! \n") sys.stdout.flush() loading_thread = threading.Thread(target=loading) loading_thread.start() try: driver.get(website) finally: # Ensure event is set even if page loading fails website_loaded.set() loading_thread.join() # Wait for loading thread to clean up console
Key improvements in this code:
- Thread-safe state management:
threading.Eventis purpose-built for inter-thread communication, making it far more reliable than a raw boolean variable. - Early loop exit: Using
website_loaded.wait(0.5)instead oftime.sleep(0.5)lets the loading thread exit immediately when the page finishes loading, no need to wait out the full sleep duration. - Clean console output: Trailing spaces in each loading string ensure longer previous states are fully overwritten, so you won't have leftover dots.
- Error-safe cleanup: The
try/finallyblock guarantees the event is set even if there's an error loading the page, and we join the thread to make sure it finishes cleaning up the console before the program exits. - Fixed URL handling: Corrected the
sys.argvusage to properly capture a single URL (adjust to' '.join(sys.argv[1:])if you need to handle URLs with spaces).
内容的提问来源于stack exchange,提问作者TheRealTengri
相关产品推荐
相关产品推荐

