如何在Power BI中使用Html.Table识别无CSS选择器的标签?
Hey there, let's tackle this Power BI HTML parsing issue you're facing! It's totally common to hit snags with inconsistent HTML structures when building reports, especially when you're learning on the job—great work troubleshooting as far as you have.
The Core Issues
First, let's break down why your current approaches aren't working:
nth-childlimitations: This selector depends on fixed row/column positions, which falls apart when table structures vary across reports. No wonder it breaks when you scale to other reports!- Incorrect
containssyntax: Your attempttd[contains='Time']uses invalid CSS syntax. The correct pseudo-class for text matching is:contains(), and even then, Power BI's web connector might have limited support for it.
Solution 1: Use a Robust CSS Selector (If Supported)
If Power BI's web connector supports the :contains() pseudo-class, you can target the "Time" label cell and then grab its adjacent sibling cell (which holds the actual time value):
{"Time", "td:contains('Time') + td"}
The + selector targets the immediate next sibling <td> right after the one containing "Time"—this works no matter which row the Time pair lives in.
Solution 2: Switch to XPath (More Reliable for Text Matching)
XPath is often more flexible for unstructured HTML, especially when dealing with text content. Use this XPath expression to precisely locate the time value:
{"Time", "//td[text()='Time']/following-sibling::td[1]"}
Let's break this down:
//td[text()='Time']: Finds any<td>element with the exact text "Time"/following-sibling::td[1]: Grabs the first immediate sibling<td>right after that label cell
If the "Time" text might have extra spaces or case variations (like " time " or "TIME"), use a case-insensitive check in XPath:
{"Time", "//td[contains(translate(text(), 'TIME', 'time'), 'time')]/following-sibling::td[1]"}
This converts the cell text to lowercase first, so it matches any case variation of "Time".
Quick Tips for Future Parsing
- Always prioritize selectors that target content (like text labels) over positional ones (
nth-child) when dealing with variable structures - Test XPath expressions directly in your browser's dev tools (F12 > Console, use
$x("your-xpath-here")to preview matches) before plugging them into Power BI - If you're unsure about selector support in Power BI, double-check the official docs for the Web.Contents function and its CSS/XPath capabilities
内容的提问来源于stack exchange,提问作者Kevin Ferrell

