Skip to content
Advertisement

Can’t locate and click on a terms of conditions button

I am new to pupeeteer and first what i am trying to do is loading a page and clicking on a button. However, it can’t locate the element. I assume this is because I need to locate the parent or parent’s parent element.

JavaScript

This is the full css selector taken from inspect

JavaScript

Here’s my code:

JavaScript

It’s a simple accept and conditions block where I would want to click on an “Accept all” button to close it and proceed further. What usual actions do I need to wait for the parent element first then dig further? Or there is an easy way?

This is the website I am trying to close terms and conditions for: https://www.partslink24.com/

Advertisement

Answer

A few problems:

  1. The selector appears dynamically after the page has loaded, so you should waitForSelector rather than assuming networkidle0 will be enough to catch the button.
  2. The selector you want is in a shadow root, so select the root and dip into it with .shadowRoot.
  3. Your .click() call must be awaited.
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement