How to click a button on a website using Puppeteer without any class, id ,... assigned to it?

18,505

Here is a way to collect that data. Try these on your browsers console first.

[...document.querySelectorAll('a.name-link')]
.filter(element => 
  element.innerText.includes('Supreme®/The North Face® Leather Shoulder Bag')
)

What's going on here?

  • document.querySelectorAll finds all element with that selector.
  • .filter will return the result that matches the query.
  • .includes will return data that includes a given string.

If a.name-link does not work, then look for a, if that does not work, then find the parent item and use that.

Once you got the element on your browser, you can apply that on your code, click it etc.

Usage:

You can use page.evaluate to filter and click.

const query = "Supreme®/The North Face® Leather Shoulder Bag";

page.evaluate(query => {
  const elements = [...document.querySelectorAll('a.name-link')];

  // Either use .find or .filter, comment one of these
  // find element with find
  const targetElement = elements.find(e => e.innerText.includes(query));

  // OR, find element with filter
  // const targetElement = elements.filter(e => e.innerText.includes(query))[0];

  // make sure the element exists, and only then click it
  targetElement && targetElement.click();
}, query)
Share:
18,505
wizencrowd
Author by

wizencrowd

Updated on June 04, 2022

Comments

  • wizencrowd
    wizencrowd almost 2 years

    So I want to click on a button on a website. The button has no id, class,... So I should find a way to click the button with the name that's on it. In this example I should click by the name "Supreme®/The North Face® Leather Shoulder Bag"

    This is my code in Node.js

    const puppeteer = require('puppeteer');
    
    let scrape = async () => {
    const browser = await puppeteer.launch({headless: false});
    const page = await browser.newPage();
    await page.goto('https://www.supremenewyork.com/shop/all/bags');
    await page.click(...);
    browser.close();
    return result;
    };
    

    This is the element that I want to click:

    <a class="name-link" href="/shop/bags/a9cz4te2r/rsth86fbl">Supreme®/The 
    North Face® Leather Shoulder Bag</a>
    
  • SIM
    SIM over 5 years
    Btw, If the elements available from your end are in reality what you have pasted above then replace this portion page.click("a[href$='a05ivugj2']") with page.click("a[href$='rsth86fbl']") from within my script above.
  • wizencrowd
    wizencrowd over 5 years
    If I change the href$ to ckf2cimj7 it will work for me. I guess this is because the website works regional. But the problem is I wont be able to get that tag before the item goes online. The only thing I will know before it goes online is the name of the item. So I need to find a way to click the link by the name "Supreme®/The North Face® Leather Shoulder Bag".
  • SIM
    SIM over 5 years
    The name you mentioned is not available in that page (at least from my end) when I search it through chrome dev tools.
  • wizencrowd
    wizencrowd over 5 years
    It is the name of the item itself. prntscr.com/l8ahbr and prntscr.com/l8ahwc
  • SIM
    SIM over 5 years
    This is how I can see check out this link.
  • wizencrowd
    wizencrowd over 5 years
    ok, that's weird. But do you got any idea how I can click the link only using the name of the item with the screenshots I gave you?
  • SIM
    SIM over 5 years
    As .querySelector() doesn't support pseudo-selectors, you need to avail xpath to click on that link defining .='Supreme®/The North Face® Leather Shoulder Bag' within it.
  • wizencrowd
    wizencrowd over 5 years
    propably a silly question but i'm kind of new to this but what do these 3 points mean before document?
  • Md. Abu Taher
    Md. Abu Taher over 5 years
  • Md. Abu Taher
    Md. Abu Taher over 5 years
    As I asked above, can you share a screenshot of the page you are trying to access? Otherwise share the source code of that page somewhere. Otherwise it's all just guessing what works etc.
  • wizencrowd
    wizencrowd over 5 years
    I removed that accepted thing by accident, because your code works. . Btw this is the link supremenewyork.com/shop/all/bags
  • Md. Abu Taher
    Md. Abu Taher over 5 years
    No, I am not talking about the link, it's about the screenshot. It doesn't show here like you are seeing. I tried with proxies too. Just doesn't show any name anywhere.
  • Md. Abu Taher
    Md. Abu Taher over 5 years
    I just guessed what you were trying to do and provided a solution based on that.
  • wizencrowd
    wizencrowd over 5 years
    This is what I see: prntscr.com/l8bhwh This is the element that I need to click on : prntscr.com/l8bioh But as you can see there are also multiple colors: prntscr.com/l8bivx I need to select the right colour too. Thanks
  • Md. Abu Taher
    Md. Abu Taher over 5 years
    Ah so Supreme shows that data for london. Great. The above snippet should work perfectly. :)
  • bibscy
    bibscy over 3 years
    I get cannot read property click of undefined