【问题标题】:How to click on a button with a data-elementid in Puppeteer?如何在 Puppeteer 中单击带有 data-elementid 的按钮?
【发布时间】:2020-11-08 18:17:13
【问题描述】:

如何在 Puppeteer 中点击带有 data-elementid 的按钮?

网站有这个:

<button class="M-r MB-r j3 Mn-cy" tabindex="0" type="button" data-elementid="searchButton">

我正在尝试点击按钮:

const selector = '#searchButton';
await page.waitForSelector(selector);
await page.click(selector);

我已尝试等待 10 秒以确保页面已完全加载。

收到错误:

TimeoutError: waiting for selector `#searchButton`

有什么想法吗?

【问题讨论】:

  • # 用于 id,data-elementid 与 id 属性不同。尝试使用选择器'[data-elementid="searchButton"]'

标签: javascript puppeteer


【解决方案1】:

问题似乎出在选择器上。

这个:

const selector = '#searchButton';

在 html 中翻译成这样:

id="searchButton"

但您正在寻找data-elementid="searchButton" 属性。

试试这个:

const selector = '[data-elementid="searchButton"]';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多