Puppeteer:在选择器中转义方括号

kas*_*hiB 0 javascript google-chrome-devtools google-cloud-functions puppeteer

我需要选择 ID 包含方括号的元素。

IE

#element[0]
Run Code Online (Sandbox Code Playgroud)

但是,我不断收到:

错误:未能找到与选择器“element[0]”匹配的元素

我已经用 转义了选择器中的元素\,但这不起作用。

page.select("#fruit\[0\]", "apples")
Run Code Online (Sandbox Code Playgroud)

双反斜杠转义也不起作用。IE:

page.select("#fruit\\[0\\]", "apples")
Run Code Online (Sandbox Code Playgroud)

更新:我试图选择的元素:

<select id="fruit[0]">
  <option>Please make a selection</option>
  <option>apples</option>
</select>
Run Code Online (Sandbox Code Playgroud)

注意:即使我尝试在上面的查询中使用 page.waitFor 方法,我也会遇到同样的错误。

Gra*_*ler 5

使用[id="fruit[0]"]:

您可以参考元素的属性使用 CSS属性选择器id:

await page.waitFor( '[id="fruit[0]"]' );

await page.select( '[id="fruit[0]"]', 'apples' );
Run Code Online (Sandbox Code Playgroud)