├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | node_modules 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2012-2014 by various contributors (see AUTHORS) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Puppeteer-select 2 | 3 | at this point its just proof of concept made for solving my own issue. 4 | Feel free to add any suggestions 5 | 6 | ## Why 7 | 8 | I got used to some useful css selectors which are not supported by puppeteer like: 9 | ``` 10 | :contains(text) 11 | ``` 12 | 13 | example from puppeteer docks modified to be using puppeteer-selector 14 | 15 | ```javascript 16 | const puppeteer = require('puppeteer'); 17 | const select = require ('puppeteer-select'); 18 | 19 | (async () => { 20 | const browser = await puppeteer.launch(); 21 | const page = await browser.newPage(); 22 | await page.goto('https://example.com'); 23 | 24 | await select(page).assertElementPresent('div.Tag:contains(Events) > button'); 25 | const element = await select(page).getElement('div.Tag:contains(Events) > button'); 26 | await element.click() 27 | 28 | await browser.close(); 29 | })(); 30 | ``` 31 | 32 | this lib is using "sizzle" 33 | all available selectors is here https://github.com/jquery/sizzle/wiki#selectors 34 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const sizzleCode = fs.readFileSync(require.resolve('sizzle')); 3 | 4 | const getElementBySizzle = eval("(selector) => { \n" + sizzleCode + "\n const elements = Sizzle(selector); return elements[0] }"); 5 | 6 | function css3Assert(instance) { 7 | 8 | const assertElementPresent = async (selector) =>{ 9 | try { 10 | const element = await instance.evaluateHandle( 11 | getElementBySizzle, 12 | selector 13 | ); 14 | if (element.asElement()) { 15 | return true 16 | } else { 17 | throw new Error('an element with selector: "'+ selector+ '" not found'); 18 | } 19 | } catch (error) { 20 | throw error 21 | } 22 | 23 | }; 24 | 25 | const getElement = async (selector) => { 26 | return await instance.evaluateHandle( 27 | getElementBySizzle, 28 | selector 29 | ); 30 | }; 31 | 32 | return { 33 | assertElementPresent: assertElementPresent, 34 | getElement: getElement 35 | }; 36 | } 37 | 38 | 39 | 40 | module.exports = css3Assert; 41 | 42 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "puppeteer-select", 3 | "version": "1.0.3", 4 | "description": "advanced css selectors for puppeteer", 5 | "repository": "https://github.com/Skadabr/puppeteer-selector", 6 | "main": "index.js", 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "keywords": [ 11 | "puppeteer", 12 | "css", 13 | "selector", 14 | "sizzle" 15 | ], 16 | "author": "Vladimir Polyakov", 17 | "license": "MIT", 18 | "dependencies": { 19 | "sizzle": "^2.3.3" 20 | } 21 | } 22 | --------------------------------------------------------------------------------