【发布时间】:2021-03-07 09:04:55
【问题描述】:
我正在尝试让 puppeteer 单击具有包含某些文本的子跨度的锚点。
HTML sn-p(个人详细信息被“...”删除):
<a data-url="/accounts/inquiry/accountdetails/..." class="account-title-group column " tabindex="0" role="link">
<i class="icon acct-tile checking"></i>
<span class="account-name OneLinkNoTx" lang="en">
Checking</span>
<span class="masked-account-number has-wells-advisor">
<span class="OneLinkNoTx">...</span>
</span>
</a>
我尝试了几种变体都无济于事:
//const aElementsWithChecking = await page.$x("//a[contains(., 'Checking')]");
//const aElementsWithChecking = await page.$x("//a[@class='account-title-group' and descendant::span[contains(., 'Checking')]]");
//const aElementsWithChecking = await page.$x("//a[@class='account-title-group' and descendant::span[contains(text(), 'Checking')]]");
//const aElementsWithChecking = await page.$x("//a[@class='account-title-group' and .//*[contains(text(), 'Checking')]]");
//const aElementsWithChecking = await page.$x("//a[@class='account-title-group' and .//*[contains(., 'Checking')]]");
const aElementsWithChecking = await page.$x("//a[(.//*|.)[.='Checking']]");
if (aElementsWithChecking.length > 0) {
await aElementsWithChecking[0].click();
} else {
throw new Error("Link not found");
}
【问题讨论】:
-
//a[contains(., 'Checking')]应该有效。如果它不在那里,您可能必须等待它。 -
我继续与 Abdel 一起进行故障排除。这个错误似乎是问题
[Report Only] Refused to run the JavaScript URL because it violates the following Content Security Policy directive: "script-src 'nonce-........' https: 'unsafe-inline' 'unsafe-eval'". Note that 'unsafe-inline' is ignored if either a hash or nonce value is present in the source list.运行page.setBypassCSP(true)并不能解决问题。 -
await page.setBypassCSP(true)?也可以尝试 args:['--disable-web-security'] 作为 puppeteer 选项
标签: javascript web-scraping puppeteer