使用Python与Selenium爬取FanTeam球员价格时无法定位元素的求助
Hey there! Let’s troubleshoot why you can’t locate those player elements on Fanteam—this is a super common snag with dynamic sites, so we’ll walk through the most likely fixes step by step:
Wait for dynamic content to finish loading
The player list is almost certainly loaded dynamically with JavaScript, so trying to grab it right afterdriver.get()will fail every time. Use explicit waits instead of arbitrarytime.sleep()calls—it’s way more reliable:from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # After navigating to the URL wait = WebDriverWait(driver, 15) # Wait up to 15 seconds (adjust if needed) player_list = wait.until(EC.presence_of_element_located((By.XPATH, "//div[@class='player-list']")))This code pauses execution until the element is present in the DOM, so you won’t get that "no such element" error anymore.
Check if the element is hidden inside an iframe
Some sites embed content in iframes, which means Selenium can’t see elements inside them unless you explicitly switch to the iframe first. Here’s how to handle that:# Locate the iframe (use browser dev tools to find the correct selector for your page) iframe = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "iframe"))) driver.switch_to.frame(iframe) # Now you can locate the player list normally player_list = wait.until(EC.presence_of_element_located((By.XPATH, "//div[@class='player-list']"))) # When you're done with the iframe, switch back to the main page if needed: # driver.switch_to.default_content()Refine your element selector
Sometimes the class name might have extra dynamic suffixes (likeplayer-list loaded) that break your exact match. Use a more flexible selector:- For XPath, use
contains()to match part of the class:player_list = wait.until(EC.presence_of_element_located((By.XPATH, "//div[contains(@class, 'player-list')]"))) - Or use a CSS selector, which is often more robust for class-based matches:
player_list = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.player-list")))
- For XPath, use
Clear cookie consent pop-ups first
Cookie banners can block page loading or obscure elements, preventing Selenium from finding what you need. Add code to accept them upfront:try: # Adjust the selector to match the site's accept button text/ID accept_cookies = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Accept All']"))) accept_cookies.click() except: # If the pop-up doesn't appear, just continue with the script pass
Start with the wait and cookie pop-up fixes first—those are the most likely culprits here. Once you’ve worked through these steps, you should be able to locate the player list and extract the prices you need.
内容的提问来源于stack exchange,提问作者Art Mich

