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

求助:使用Selenium Python无法获取网页表格全部<tr>元素仅能获取第一行

Fix: Selenium Only Retrieves First Table Row <tr> Element

Hey there, I’ve run into this exact issue before—when the browser’s dev tools can see all your table rows but Selenium only grabs the first one, it’s usually due to one of a few common gotchas. Let’s walk through the fixes step by step:

1. Check if the table is inside an <iframe>

A super common culprit! If your table is nested inside an iframe, Selenium operates in the main document context by default and can’t see elements inside the iframe. Here’s how to fix it:

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

# Locate the iframe first (use its ID, name, or XPath)
iframe = WebDriverWait(browser, 20).until(
    EC.presence_of_element_located((By.ID, "your_iframe_id"))
)
# Switch Selenium's context to the iframe
browser.switch_to.frame(iframe)

# Now try grabbing your table rows again
rows = WebDriverWait(browser, 20).until(
    EC.presence_of_all_elements_located((By.XPATH, "//*[@id='tableDay']/tbody/tr"))
)
print(f"Total rows found: {len(rows)}")
for row in rows:
    print(row.text)

# Don't forget to switch back to the main document if you need to interact with other elements later
browser.switch_to.default_content()

2. Handle lazy/dynamic loading

Some tables load rows only when they scroll into view. Even if your dev tools show all rows, Selenium might be trying to grab them before they’re fully rendered. Try scrolling to the bottom of the table first:

import time

# Locate the table container
table = browser.find_element(By.ID, "tableDay")
# Scroll to the bottom of the table to trigger all rows to load
browser.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", table)
# Wait a moment for rows to load (or use explicit wait for better reliability)
time.sleep(2)

# Now fetch all rows—use presence_of_all_elements_located instead of visibility if rows are off-screen
rows = WebDriverWait(browser, 20).until(
    EC.presence_of_all_elements_located((By.XPATH, "//*[@id='tableDay']/tbody/tr"))
)
print(f"Total rows found: {len(rows)}")
for row in rows:
    print(row.text)

3. Simplify your locator (avoid tbody confusion)

Sometimes tables have nested <tbody> tags, or your XPath is targeting the wrong one. Skip the intermediate tbody step and target all rows directly under the table:

rows = WebDriverWait(browser, 20).until(
    EC.presence_of_all_elements_located((By.XPATH, "//table[@id='tableDay']//tr"))
)

The // after the table ensures we grab every <tr> element at any level inside the table, no matter how the tbody is structured.

4. Bypass Selenium locators entirely with JavaScript

Since your browser’s console can see all 37 rows, use Selenium to execute the same JavaScript logic directly. This bypasses any locator quirks:

# Execute JS to get all row text content
row_texts = browser.execute_script("""
    const rows = document.querySelectorAll("#tableDay tbody tr");
    return Array.from(rows).map(row => row.textContent.trim());
""")

print(f"Total rows found: {len(row_texts)}")
for text in row_texts:
    print(text)

This method pulls exactly what your browser sees, so it should work if the rows exist in the DOM.

Give these steps a try—one of them should get you all those missing table rows!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 21:19:08