使用谓词谓词的 html 选择器
我正在与Puppeteer一起工作,我想通过寻找带有特定标题名称的TD来选择一个TR。
我尝试了:
const targetPage = page;
const td = 'td[title="18to22"]';
const td1 = await waitForSelectors([[td]], targetPage, timeout);
async function waitForSelectors(selectors, frame, timeout) {
for (const selector of selectors) {
try {
return await waitForSelector(selector, frame, timeout);
} catch (err) {
console.error(err);
}
}
throw new Error(
"Could not find element for selectors: " + JSON.stringify(selectors)
);
}
哪个有效。现在,我想获得父行(tr)
我该怎么做?
I'm working with puppeteer , I want to select a tr by looking for a td with a specific title name.
I tried:
const targetPage = page;
const td = 'td[title="18to22"]';
const td1 = await waitForSelectors([[td]], targetPage, timeout);
async function waitForSelectors(selectors, frame, timeout) {
for (const selector of selectors) {
try {
return await waitForSelector(selector, frame, timeout);
} catch (err) {
console.error(err);
}
}
throw new Error(
"Could not find element for selectors: " + JSON.stringify(selectors)
);
}
which works. Now I want to get the parent row (tr)
How do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能纯粹用CSS选择器来解决它(
:has( )
到目前为止,“支持”很差)。我会使用
page .$eval
与Node.parentElement
(我还推荐 可选链接,但是当然通常有一个
parent)
在下面的示例中,我检索了它的
innerText
,当然您可以根据需要使用节点元素。You cannot solve it purely with CSS selectors (
:has()
"has" a poor support so far).I'd use
page.$eval
togethet withNode.parentElement
(I also recommend optional chaining, but of course a<td>
usually has a<tr>
parent)In the below example I retreive its
innerText
, of course you can use the node element as you need it.