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]: spawn ENAMETOOLONG HOT 8
- [Bug]: element disappears when cross the bound of the PDF footer HOT 5
- [Bug] puppeteer.launch with cmd file cause spawn EINVAL with recent node security patch HOT 7
- [Bug]: emoji font not displayed (just blank spaces) HOT 42
- [Bug]: `PerformanceNavigationTiming.type` is `navigation` after pressing back button HOT 7
- [Bug]: Error: net::ERR_FAILED HOT 2
- [Feature]: Let docker/Dockerfile use node:20-slim as the base image HOT 2
- [Bug]: #<NoSuchFrameException> occurs even when manually navigating to a page while the browser is idle in puppeteer HOT 2
- [Bug]: Error: Could not find Chrome (ver. 124.0.6367.78). HOT 2
- [Bug]: Instructions can be improved/corrected HOT 2
- Switch to macos-latest (arm64) in ci
- [Feature]: Request Initiator Chain HOT 8
- [Bug]: Could not find Chrome (ver. ${this.puppeteer.browserRevision}) on Docker HOT 7
- [Bug]: 'page.waitForTimeout' not working in Puppeteer 22.7.1 HOT 2
- [Bug]: use manifest V3 .When the file(manifest.json) carries ”content_scripts“,puppeteer.launch timeout HOT 16
- "[network.spec] network Request.resourceType should work for document type" is flaky in firefox
- ignoreHttpsErrors should be renamed to acceptInsecureCerts
- [Bug]: Redirect stops before the final destination that has an error Name Not Resolved HOT 4
- Node process is hanging after all bidi tests are finished HOT 1
- AWS lambda: running puppeteer-core 21.5.2 and @sparticuz/chromium 118.0.0 on node 18 runtime failes ONLY when my html (for PDF rendering) contains https HOT 1
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.