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

Selenium获取指定ess-cell元素失败求助:层级查找代码无效原因排查

Hey there! Let's figure out why your Selenium code isn't picking up those ess-cell elements with the data-numeric class, even though the first two div elements are found successfully. Here are the most likely culprits and fixes:

1. The ess-cell elements aren't loaded yet

Web pages often load content dynamically—your first two divs might render quickly, but the ess-cell components could be loading asynchronously after that. When you call find_elements_by_tag_name right away, those elements simply aren't present in the DOM yet.

Fix: Use explicit waits to wait for the elements to load before trying to grab them. This is way more reliable than relying on implicit waits or arbitrary time sleeps:

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

# Wait up to 10 seconds for all target ess-cell elements to appear
target_cells = WebDriverWait(row, 10).until(
    EC.presence_of_all_elements_located((By.CSS_SELECTOR, "ess-cell.data-numeric"))
)

2. Your hierarchy path is incorrect

When you use find_element_by_tag_name('div') twice, you're grabbing the first div at each level—but the ess-cell elements might be nested under a different div in the row, not the first two. For example, there could be sibling divs in the row, and your current code is drilling into the wrong one.

Fix: Debug the hierarchy by printing the HTML of each step to see what you're working with:

# Check what the first div contains
first_div = row.find_element_by_tag_name('div')
print(first_div.get_attribute('outerHTML'))

# Check what the second div contains
second_div = first_div.find_element_by_tag_name('div')
print(second_div.get_attribute('outerHTML'))

If the printed HTML doesn't show any ess-cell elements, you'll know your path is off. Instead of relying on tag names alone, use more specific selectors to target the correct parent div (like its class or ID).

3. Custom element quirk (less likely, but worth checking)

Since ess-cell is a custom HTML element, in rare cases Selenium might need a little extra nudge to recognize it. Switching to a CSS selector or XPath that directly targets the element and its class is usually more reliable than chaining tag name lookups.

Alternative fix with XPath:

target_cells = row.find_elements(By.XPATH, ".//ess-cell[contains(@class, 'data-numeric')]")

The .// in the XPath ensures you're only looking for elements inside the row element, which matches your original intent.

Start by debugging the hierarchy to confirm your path is correct—if the second div truly should contain the ess-cells, then dynamic loading is almost certainly the issue, and the explicit wait will fix it.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 13:48:13