Comments (7)
Why do you want to press enter? Usually there is an equally clickable element.
from puppeteer.
let Browser = require('../lib/Browser.js');
let browser = new Browser({ args: ['--no-sandbox'] });
browser.newPage().then(async page => {
await page.navigate('https://www.google.com');
let query = 'input[title="Search"]';
page.focus(query);
page.type('blin');
page.keyboard.press('Enter');
await page.waitFor('h3 a');
let urls = await page.$$('h3 a', a => a.href);
let i = 0;
for (let url of urls) {
i++;
await page.navigate(url);
await page.screenshot({
path: i + '.png'
});
}
browser.close();
});
page.waitFor
didn't work for me. Added text nodes crash it, and a new node could make the selector resolve anywhere in the document.
from puppeteer.
- Running your script on a Mac, it does not work. I get stuck with blink| in the search field.
- Also, this script does not do what I want - I want to be clicking search results, not navigating manually. That covers the back/forward use cases
- And to add stress to it, I want to be clicking the search button. It is in DOM all the time, but invisible half of the time which adds fun!
from puppeteer.
The page.waitFor
was broken and is now fixed in 28265fc
A few observations:
- For some reason, google.com handles "Enter" in input field differently every other time. Sometimes it issues a navigation to the
https://www.google.com/search?q=blin
endpoint, and sometimes it behaves as SPA and navigates tohttps://www.google.com/#q=Blin
. - Opening multiple tabs and navigating them altogether doesn't work - half of the tabs timeout navigation
- Trying to take screenshot simultaneously from multiple tabs doesn't work good - screenshotting hangs
The following works for me, but I don't really click the links and don't really click the "search" button. But we'll fix it!
const {Browser} = new require('.');
const browser = new Browser({headless: false});
const VIEWPORT = {width: 1000, height: 600};
browser.newPage().then(async page => {
await page.setViewport(VIEWPORT);
await page.navigate('https://google.com');
await page.focus('#lst-ib');
await page.type('Blin');
await page.keyboard.press('Enter');
try {
await page.waitFor('div.g');
} catch(e) {
// Page might issue a navigation, so the page.waitFor will throw.
await page.waitFor('div.g');
}
await page.screenshot({path: 'ee.png'});
let links = await page.$$('div.g h3 > a', link => link.href);
// have to do this sequentially =/
for (let i = 0; i < links.length; ++i)
await screenshotURL(links[i], `${i}.png`);
browser.close();
});
async function screenshotURL(url, name) {
let page = await browser.newPage();
await page.setViewport(VIEWPORT);
try {
await page.navigate(url, { maxTime: 5000 });
} catch (e) {
// we did our best.
}
await page.screenshot({path: name});
page.close();
console.log('Done: ' + name);
}
from puppeteer.
After the offline discussion of the stress-test, here are the bullets:
- make mouse to scroll element into view before clicking (otherwise it doesn't work!)
- introduce
page.press
as a shortcut forpage.keyboard.press
- don't do any network throttling other than offline mode. We're bad at emulating it
- implement
page.waitForVisible(selector)
which comes handy - implement
page.waitForNavigation
- implement
page.back
/page.forward
- improve support for DEBUG module for public api methods
- make screenshot tasks per browser, not per page. Screenshotting doesn't work across multiple tabs
- screenshots on mac seem to be unstable
- rename the keyboard's
hold
intodown
, andrelease
intoup
-
page.waitForSelector
should survive navigation (currently it doesn't) -
page.waitForSelector
should timeout after some time (e.g. 30s by default)
from puppeteer.
The resulting script looks like this:
const {Browser} = require('puppeteer');
const browser = new Browser({headless: false});
browser.newPage().then(async page => {
page.on('load', () => console.log('LOADED: ' + page.url()));
await page.navigate('https://google.com');
await page.waitFor('input[name=q]');
await page.focus('input[name=q]');
await page.type('blin');
await page.press('Enter');
for (let i = 0; i < 10; ++i) {
let searchResult = `div.g:nth-child(${i + 1}) h3 a`;
await page.waitFor(searchResult, {visible: true});
page.click(searchResult);
await page.waitForNavigation();
await page.screenshot({path: `screenshot-${i + 1}.png`});
await page.goBack();
}
browser.close();
});
The two checkboxes from feedback which are not addressed are filed separately:
- "screenshots on mac seem to be unstable" - which is #100
- "don't do any network throttling other than offline mode. We're bad at emulating it" - which is #63
from puppeteer.
You might be in different buckets too. I'm sure google is doing A/B testing. There's a way to force a bucket, but I'm not sure how.
from puppeteer.
Related Issues (20)
- [Bug]: Refused to set unsafe header HOT 4
- [Bug]: No command (timeout) cleanup when when a page closes HOT 2
- [Feature]: Migrate from postData to postDataEntries in network requests
- [Feature]: Update the https://pptr.dev/chromium-support page
- Improve documentation site HOT 2
- VERCEL NEXT JS: Failed to launch the browser process! HOT 1
- `browsingContext.create` fails after abandoned `browsingContext.print` via `browsingContext.close` in Firefox over BiDi HOT 1
- [Bug]: /node_modules/puppeteer-core/lib/cjs/puppeteer/util/disposable.js | Symbol.dispose ??= Symbol('dispose'); | SyntaxError: Unexpected token '??=' HOT 2
- Inquiry: ARM Compatibility for Puppeteer HOT 1
- [Bug]: using exposeFunction on a page with an iframe pointing to a pdf causes hang HOT 7
- [Bug]: PDF - Currency symbol not displaying(displaying as a small box) while taking print out from Ubuntu server HOT 8
- Release 22.6.2 has not been published HOT 1
- Update automation tools HOT 1
- [Bug]: HOT 5
- [Bug]: Final status code changes when request intercept is TRUE HOT 7
- how to play audio in puppeteer join google meet in a way it can be audible to everyone HOT 1
- [Bug]: Last version 22.6.2 is not published on npm registry HOT 2
- [Bug]: Browser.close does not shutdown the process immediately HOT 16
- [Bug]: MaxListenersExceededWarning caused by process.on('exit') HOT 4
- [Feature]: ability to check and possibly installed missing system dependencies
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from puppeteer.