如何解决Selenium中TimeoutException报错?爬虫代码排查求助
Here's how to resolve the TimeoutException and successfully extract team names from the site:
Common Causes & Fixes
1. Cookie Consent Popup Blocking Content
Most betting sites require cookie consent before loading core content. If you skip this step, target elements may never render. Add code to accept cookies first:
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 url_getsbet = 'https://online.getsbet.ro/sports' driver = webdriver.Chrome() driver.get(url_getsbet) # Accept cookie consent (adjust selector if the popup uses a different class/ID) try: accept_cookies_btn = WebDriverWait(driver, 5).until( EC.element_to_be_clickable((By.CSS_SELECTOR, "button.cookie-bar__button--accept")) ) accept_cookies_btn.click() except: # Proceed if no cookie popup appears pass
2. Use Visibility Check Instead of Presence
presence_of_all_elements_located only verifies elements exist in the DOM, but they might still be hidden. Switch to visibility_of_all_elements_located to ensure elements are visible on the page:
# Wait for match participant containers to become visible matches_sb = WebDriverWait(driver, 15).until( EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "span.Details__Participants")) )
3. Extract Actual Team Names
Your current code prints WebElement objects, not the team names. Target the inner Details__ParticipantName spans to get the text:
# Loop through matches and extract home/away team names for match in matches_sb: home_team = match.find_element(By.CSS_SELECTOR, ".Details__Participant--Home .Details__ParticipantName").text away_team = match.find_element(By.CSS_SELECTOR, ".Details__Participant--Away .Details__ParticipantName").text print(f"{home_team} vs {away_team}")
4. Check for Iframes
If elements still aren't found, inspect the page to see if they're nested inside an iframe. If so, switch to the iframe first:
# Switch to the target iframe (adjust selector to match the site's actual iframe) iframe = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CSS_SELECTOR, "iframe[src*='sports']")) ) driver.switch_to.frame(iframe) # Now proceed to locate match elements
5. Handle Dynamic Loading via Scrolling
Some sites load content as you scroll. If teams are below the fold, trigger loading by scrolling to the bottom:
# Scroll to bottom of the page to load all matches driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # Short wait for content to load driver.implicitly_wait(3)
Full Revised Code
Combining all fixes into one working script:
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 url_getsbet = 'https://online.getsbet.ro/sports' driver = webdriver.Chrome() driver.get(url_getsbet) # Accept cookies try: accept_btn = WebDriverWait(driver, 5).until( EC.element_to_be_clickable((By.CSS_SELECTOR, "button.cookie-bar__button--accept")) ) accept_btn.click() except: pass # Wait for visible match containers matches = WebDriverWait(driver, 15).until( EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "span.Details__Participants")) ) # Extract and print team names for match in matches: home = match.find_element(By.CSS_SELECTOR, ".Details__Participant--Home .Details__ParticipantName").text away = match.find_element(By.CSS_SELECTOR, ".Details__Participant--Away .Details__ParticipantName").text print(f"{home} vs {away}") driver.quit()
Key Notes
- Use browser dev tools (F12) to confirm selectors, check for iframes, or identify anti-scraping measures.
- Adjust wait times (e.g., 15 seconds in
WebDriverWait) if the page loads slowly. - If anti-scraping blocks you, try adding small delays or spoofing a user-agent string.
内容的提问来源于stack exchange,提问作者Florentin-Alexandru Iosif

