-
-
Save mrhung/6d5c6124e1814b71de962dbe5f934f94 to your computer and use it in GitHub Desktop.
Click link by text in Puppeteer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const puppeteer = require('puppeteer'); | |
| const escapeXpathString = str => { | |
| const splitedQuotes = str.replace(/'/g, `', "'", '`); | |
| return `concat('${splitedQuotes}', '')`; | |
| }; | |
| const clickByText = async (page, text) => { | |
| const escapedText = escapeXpathString(text); | |
| const linkHandlers = await page.$x(`//a[contains(text(), ${escapedText})]`); | |
| if (linkHandlers.length > 0) { | |
| await linkHandlers[0].click(); | |
| } else { | |
| throw new Error(`Link not found: ${text}`); | |
| } | |
| }; | |
| const run = async () => { | |
| const browser = await puppeteer.launch({args: ['--no-sandbox'], headless: true}); | |
| const page = await browser.newPage(); | |
| await page.goto('https://en.wikipedia.org/wiki/List_of_The_Sandman_characters'); | |
| await clickByText(page, `Fiddler's Green`); | |
| await page.waitForNavigation({waitUntil: 'load'}); | |
| console.log("Current page:", page.url()); | |
| return browser.close(); | |
| }; | |
| const logErrorAndExit = err => { | |
| console.log(err); | |
| process.exit(); | |
| }; | |
| run().catch(logErrorAndExit); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/tokland/d3bae3b6d3c1576d8700405829bbdb52