Puppeteer尽管页面上有按钮,但未能点击。

huangapple go评论65阅读模式
英文:

Puppeteer isn't clicking a button despite it being on the page

问题

Bug expectation:

我期望在点击“Dismiss”按钮之后,在Google Meet页面上点击“Join Now”按钮。

Any help would be greatly appreciated.

这里是我尝试点击的DOM节点:

<button
  class="VfPpkd-LgbsSe VfPpkd-LgbsSe-OWXEXe-k8QpJ VfPpkd-LgbsSe-OWXEXe-dgl2Hf nCP5yc AjY5Oe DuMIQc LQeN7 jEvJdc QJgqC"
  jscontroller="soHxf"
  jsaction="click:cOuCgd; mousedown:UX7yZ; mouseup:lbsD7e; mouseenter:tfO1Yc; mouseleave:JywGue; touchstart:p6p2H; touchmove:FwuNnf; touchend:yfqBxc; touchcancel:JMtRjd; focus:AHmuwe; blur:O22p3e; contextmenu:mg9Pef;mlnRJb:fLiPzd;"
  data-idom-class="nCP5yc AjY5Oe DuMIQc LQeN7 jEvJdc QJgqC"
  jsname="Qx7uuf"
>
  <div class="VfPpkd-Jh9lGc"></div>
  <div class="VfPpkd-J1Ukfc-LhBDec"></div>
  <div class="VfPpkd-RLmnJb"></div>
  <span jsname="V67aGc" class="VfPpkd-vQzf8d">Join now</span>
</button>

Puppeteer代码在示例部分。

我还尝试过按文本获取节点,使用 text/Join Now,但是收到以下错误:

Error: Query set to use "text", but no query handler of that name was found
    at Object.getQueryHandlerAndSelector

Bug behavior:

  • 不稳定
  • PDF

Minimal, reproducible example:

const joinMeeting = async (page) => {
    console.log(logYellow, '📺 Joining meeting...');
    await page.goto('https://meet.google.com/');
    await page.waitForSelector('.mobgod', { visible: true });
    await page.click('.mobgod');
    await page.waitForSelector('.VfPpkd-vQzf8d');
    await page.click('.VfPpkd-vQzf8d'); // 点击 Dismiss 按钮
    await page.waitForSelector('.VfPpkd-LgbsSe');
    await page.click('.VfPpkd-LgbsSe'); // 点击 Join 按钮
    console.log(logGreen, ' Joined meeting!');
}

Error string:

没有错误。只是在我需要点击按钮的部分停在那里。

Puppeteer配置:

没有回应

Puppeteer版本:

10.4.0

Node版本:

18.16.0

包管理器:

pnpm

包管理器版本:

8.6.1

操作系统:

macOS

英文:

Bug expectation

I expected the 'Join Now' button to be clicked on the Google Meet page after the 'Dismiss' button has been clicked.

Any help would be greatly appreciated.

Here is the dom node I am trying to click:

<button
  class="VfPpkd-LgbsSe VfPpkd-LgbsSe-OWXEXe-k8QpJ VfPpkd-LgbsSe-OWXEXe-dgl2Hf nCP5yc AjY5Oe DuMIQc LQeN7 jEvJdc QJgqC"
  jscontroller="soHxf"
  jsaction="click:cOuCgd; mousedown:UX7yZ; mouseup:lbsD7e; mouseenter:tfO1Yc; mouseleave:JywGue; touchstart:p6p2H; touchmove:FwuNnf; touchend:yfqBxc; touchcancel:JMtRjd; focus:AHmuwe; blur:O22p3e; contextmenu:mg9Pef;mlnRJb:fLiPzd;"
  data-idom-class="nCP5yc AjY5Oe DuMIQc LQeN7 jEvJdc QJgqC"
  jsname="Qx7uuf"
>
  <div class="VfPpkd-Jh9lGc"></div>
  <div class="VfPpkd-J1Ukfc-LhBDec"></div>
  <div class="VfPpkd-RLmnJb"></div>
  <span jsname="V67aGc" class="VfPpkd-vQzf8d">Join now</span>
</button>

The puppeteer code is in the example section.

I have also tried grabbing the node by text, using text/Join Now however I receive the following error:

Error: Query set to use "text", but no query handler of that name was found
    at Object.getQueryHandlerAndSelector

Bug behavior

  • Flaky
  • PDF

Minimal, reproducible example

const joinMeeting = async (page) => {
    console.log(logYellow, '📺 Joining meeting...');
    await page.goto('https://meet.google.com/');
    await page.waitForSelector('.mobgod', { visible: true });
    await page.click('.mobgod');
    await page.waitForSelector('.VfPpkd-vQzf8d');
    await page.click('.VfPpkd-vQzf8d'); // Click dismiss buttong
    await page.waitForSelector('.VfPpkd-LgbsSe');
    await page.click('.VfPpkd-LgbsSe'); // Click join button
    console.log(logGreen, ' Joined meeting!');
}

Error string

No error. Just hangs at the section where I need it to click the button.

Puppeteer configuration

No response

Puppeteer version

10.4.0

Node version

18.16.0

Package manager

pnpm

Package manager version

8.6.1

Operating system

macOS

答案1

得分: 0

我认为你应该尽量以更通用的方式定位元素,看起来谷歌根据页面更改这些类名,因此定位.VfPpkd-vQzf8d永远不会是可靠的。

这里有一个对我有效的示例,使用了更通用的选择方法和evaluate来确保页面上的JS正常运行:

const joinMeeting = async (page) => {
    console.log('🚪 Joining meeting...');
    await page.goto('https://meet.google.com/');
    
    // 选择一些更通用的内容,至少在一段时间内是一致的。
    const eventActionValue = 'start a meeting';
    const elementSelector = `[event-action="${eventActionValue}"]`;
    
    // 等待元素出现。
    const button = await page.waitForSelector(elementSelector);

    // 而不是直接尝试点击,我们尝试在页面上下文中运行点击处理程序。这应该允许页面上的JS执行其操作并正确处理事件。
    await button.evaluate(b => b.click());
    
    // 你可以以类似的方式继续。等待下一页加载,使用更通用的方法找到你的元素等。
}

希望对你有所帮助 Puppeteer尽管页面上有按钮,但未能点击。

英文:

I think you should try to target your elements more generically, it looks like google changes these classnames depending on the page, so targeting .VfPpkd-vQzf8d is never gonna be reliable.

Here is an example of something that worked for me that uses a bit more generic selection method and evaluate to allow the JS handling on the page to run properly:

const joinMeeting = async (page) => {
    console.log( '📺 Joining meeting...');
    await page.goto('https://meet.google.com/');
    
    // Choosing something a bit more generic here that would be consistent for at least a bit.
    const eventActionValue = 'start a meeting';
    const elementSelector = `[event-action="${eventActionValue}"]`;
    
    // Wait for the element to appear.
    const button = await page.waitForSelector(elementSelector);

    // Instead of trying to click direclty, we try to run the click handler in the page context. This should allow the
    // JS on the page to do it's thing and handle the event properly.
    await button.evaluate(b => b.click());
    
    // You can continue in a similar manner. Waiting for next page load, finding your element with something more generic etc.
}

Hope that helps Puppeteer尽管页面上有按钮,但未能点击。

答案2

得分: 0

我只需要选择页面上的所有按钮,然后使用它的索引选择按钮。

英文:

All I needed to do was select all the buttons on the page, and then select the button using it's index.

huangapple
  • 本文由 发表于 2023年6月13日 17:48:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定