Selenium无法抓取图片问题求助
Hey there, let's break down why your code isn't picking up the image elements and fix it together!
First, Fix the Deprecated Locator Method
You're seeing a deprecation warning for find_elements_by_tag_name — this method is no longer supported in newer Selenium versions. Let's replace it with the modern syntax:
from selenium.webdriver.common.by import By # Replace this line images = driver.find_elements_by_tag_name('img') # With this images = driver.find_elements(By.TAG_NAME, 'img')
Replace time.sleep() with Explicit Waits
Fixed waits like time.sleep() are unreliable because page load times can vary. Instead, use Selenium's explicit waits to wait until the image elements are actually present in the DOM:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Wait up to 10 seconds for img elements to be present wait = WebDriverWait(driver, 10) images = wait.until(EC.presence_of_all_elements_located((By.TAG_NAME, 'img')))
This ensures your code only tries to access the images once they've loaded, not just after an arbitrary time delay.
Check if Images Are Inside an iframe
If the image is nested inside an <iframe> tag, Selenium can't access it directly — you need to switch to the iframe first:
# Locate the iframe (use appropriate selector, e.g., by ID, name, or tag) iframe = wait.until(EC.presence_of_element_located((By.TAG_NAME, 'iframe'))) driver.switch_to.frame(iframe) # Now try to find the images images = wait.until(EC.presence_of_all_elements_located((By.TAG_NAME, 'img'))) # After you're done with the iframe, switch back to the main content driver.switch_to.default_content()
Debugging Steps to Verify
If you're still stuck, try these quick checks:
- Print the full page source to confirm the img elements are loaded:
Search the output forprint(driver.page_source)<imgto see if the elements are actually present when your code runs. - Take a screenshot of the page to see what Selenium is seeing:
This can reveal if the page hasn't fully rendered, or if the image is hidden behind a loader.driver.save_screenshot('loaded_page.png')
Modified Full Code
Putting it all together, your code would look like this:
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 driver = webdriver.Chrome('chromedriver') begining_of_url = "https://lookup.guru/" whole_url = begining_of_url + str(target_id) driver.get(whole_url) wait = WebDriverWait(driver, 10) # Check for iframes first (remove this block if no iframe exists) try: iframe = wait.until(EC.presence_of_element_located((By.TAG_NAME, 'iframe'))) driver.switch_to.frame(iframe) except: # No iframe found, proceed with main content pass # Wait for images to load images = wait.until(EC.presence_of_all_elements_located((By.TAG_NAME, 'img'))) pfp = None if images: pfp = images[0].get_attribute('src') print(pfp) else: print("No image elements found!") driver.quit()
内容的提问来源于stack exchange,提问作者Thomas

