木偶:按HTML属性查找元素

时间:2019-08-28 11:36:40

标签: javascript node.js puppeteer

本质上,我试图让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>

我尝试了一些方法,但是似乎找不到解决方案,我们将不胜感激!

1 个答案:

答案 0 :(得分:1)

查看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"]');

  // ...
});