Git Product home page Git Product logo

Comments (7)

JoelEinbinder avatar JoelEinbinder commented on April 28, 2024

Why do you want to press enter? Usually there is an equally clickable element.

from puppeteer.

JoelEinbinder avatar JoelEinbinder commented on April 28, 2024
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.

pavelfeldman avatar pavelfeldman commented on April 28, 2024
  • 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.

aslushnikov avatar aslushnikov commented on April 28, 2024

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 to https://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.

aslushnikov avatar aslushnikov commented on April 28, 2024

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 for page.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 into down, and release into up
  • page.waitForSelector should survive navigation (currently it doesn't)
  • page.waitForSelector should timeout after some time (e.g. 30s by default)

from puppeteer.

aslushnikov avatar aslushnikov commented on April 28, 2024

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.

ralyodio avatar ralyodio commented on April 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.