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

Selenium Python抓取<td>元素返回空文本,无法提取2016年份求助

Hi Anna, sorry to hear you're stuck grabbing that 2016 value—let's walk through some common fixes for this issue!

Common Reasons & Solutions

1. The element isn't fully loaded yet

Selenium might be trying to access the element before the page finishes rendering. Try adding an explicit wait to give the page time to load the content:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# Wait up to 10 seconds for the element to become visible
year_element = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.XPATH, "//*[@class = 'zsg-table yui3-toggle-content-minimized']/tbody/tr[1]/td[1]"))
)
print(year_element.text)

2. The element is inside an iframe

If your target table lives inside an <iframe> tag, you need to switch to the iframe first before accessing the element:

# Locate the iframe (adjust the selector to match your actual iframe)
iframe = driver.find_element(By.CSS_SELECTOR, "iframe[src*='your-iframe-source']")
driver.switch_to.frame(iframe)

# Now try fetching the year element
year_element = driver.find_element(By.XPATH, "//*[@class = 'zsg-table yui3-toggle-content-minimized']/tbody/tr[1]/td[1]")
print(year_element.text)

# Switch back to the main page if you need to interact with other elements later
driver.switch_to.default_content()

3. The text is dynamically injected via JavaScript

Sometimes the .text property doesn't capture content added by JS. Try pulling the innerText or textContent directly via JavaScript:

year_element = driver.find_element(By.XPATH, "//*[@class = 'zsg-table yui3-toggle-content-minimized']/tbody/tr[1]/td[1]")
# Fetch innerText
year_text = driver.execute_script("return arguments[0].innerText;", year_element)
# Or try textContent if innerText fails
# year_text = driver.execute_script("return arguments[0].textContent;", year_element)
print(year_text)

4. Your XPath might be inaccurate

Double-check your XPath in the browser's DevTools (F12 > Elements tab > Ctrl+F to search):

  • The zsg-table yui3-toggle-content-minimized class might have extra dynamic classes attached. Use contains() to match partial classes:
    year_element = driver.find_element(By.XPATH, "//table[contains(@class, 'zsg-table') and contains(@class, 'yui3-toggle-content-minimized')]/tbody/tr[1]/td[1]")
    
  • Verify that tr[1]/td[1] is actually pointing to the cell with 2016—sometimes tables have hidden rows or headers that shift the index.

If none of these work, check if the element is hidden (e.g., display: none in CSS). In that case, use presence_of_element_located instead of visibility_of_element_located, then fetch the text via JS as shown above.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:37:37