本质上,我试图让 Puppeteer 通过其属性 data-form-field-value 查找此页面上的元素,该属性必须等于 244103310504090。
这是按钮的 HTML 代码:
<section class="fl-accordion-tab--content">
<div class="fl-product-size">
</button><button class="fl-product-size--item fl-product-size--item__is-
selected" type="button" data-form-field-target="SKU" data-form-field-base-css-
name="fl-product-size--item" data-form-field-value="244103310504090" data-form-
field-unselect-group="" data-testid="fl-size-244103310504-US-9" data-product-
size-select-item="244103310504090" data-form-field-selected="true">
<span>9</span>
</button></div>
</section>
我尝试了一些方法,但似乎找不到解决方案,感谢任何帮助!
最佳答案
查看documentation对于 Puppeteer,您需要为您指定的属性构造一个元素选择器。
attribute selectors 的 MDN 文档解释如何执行此操作:
const puppeteer = require('puppeteer');
puppeteer.launch().then(async browser => {
const page = await browser.newPage();
await page.goto('https://example.com');
// Select the element using the attribute
const element = await page.$('[data-form-field-value="244103310504090"]');
// ...
});
关于javascript - Puppeteer:通过 HTML 属性查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57691509/