Vzupo
Vzupo

Reputation: 1468

puppeteer.js click hyperlink with a class

async function main() {
    const browser = await puppeteer.launch({ headless: false});
    const page = await browser.newPage();

    await page.goto(url);
    console.log(await page.title());

await page.click('a.search-name');

}
<div class="search-results-content-container">
                    <div class="search-results-image-container">
                    </div>
                    <div class="search-results-details-container">
                         <h4 class="search-name"><a href="http://yahoo.com">Doe, Johm</a></h4>
                              <p class="search-title">
                                  center of art
                              </p>
                    </div>

Hi I am using puppeteer to automate some tasks and I am moving along until it is time to click a h4 link that has a class called 'search-name'. I have tried adding await page.click('a.search-name') but that does not work. I also tried await page.click('h4.search-name') but no luck either. Can someone tell me how to make it click a link with an interchangeable url. It is the reason I want to specifically click h4 with a class of search-name.

Upvotes: 6

Views: 11973

Answers (1)

Vzupo
Vzupo

Reputation: 1468

The below code is what works!

await page.waitForSelector('.search-name')
await page.click('.search-name');

Upvotes: 7

Related Questions