Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. # Puppeteer
  2. <!-- [START badges] -->
  3. [![Build status](https://img.shields.io/travis/com/puppeteer/puppeteer/main.svg)](https://travis-ci.com/puppeteer/puppeteer) [![npm puppeteer package](https://img.shields.io/npm/v/puppeteer.svg)](https://npmjs.org/package/puppeteer) [![Issue resolution status](https://isitmaintained.com/badge/resolution/puppeteer/puppeteer.svg)](https://github.com/puppeteer/puppeteer/issues)
  4. <!-- [END badges] -->
  5. <img src="https://user-images.githubusercontent.com/10379601/29446482-04f7036a-841f-11e7-9872-91d1fc2ea683.png" height="200" align="right">
  6. ###### [API](https://github.com/puppeteer/puppeteer/blob/v5.5.0/docs/api.md) | [FAQ](#faq) | [Contributing](https://github.com/puppeteer/puppeteer/blob/main/CONTRIBUTING.md) | [Troubleshooting](https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md)
  7. > Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the [DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/). Puppeteer runs [headless](https://developers.google.com/web/updates/2017/04/headless-chrome) by default, but can be configured to run full (non-headless) Chrome or Chromium.
  8. <!-- [START usecases] -->
  9. ###### What can I do?
  10. Most things that you can do manually in the browser can be done using Puppeteer! Here are a few examples to get you started:
  11. * Generate screenshots and PDFs of pages.
  12. * Crawl a SPA (Single-Page Application) and generate pre-rendered content (i.e. "SSR" (Server-Side Rendering)).
  13. * Automate form submission, UI testing, keyboard input, etc.
  14. * Create an up-to-date, automated testing environment. Run your tests directly in the latest version of Chrome using the latest JavaScript and browser features.
  15. * Capture a [timeline trace](https://developers.google.com/web/tools/chrome-devtools/evaluate-performance/reference) of your site to help diagnose performance issues.
  16. * Test Chrome Extensions.
  17. <!-- [END usecases] -->
  18. Give it a spin: https://try-puppeteer.appspot.com/
  19. <!-- [START getstarted] -->
  20. ## Getting Started
  21. ### Installation
  22. To use Puppeteer in your project, run:
  23. ```bash
  24. npm i puppeteer
  25. # or "yarn add puppeteer"
  26. ```
  27. Note: When you install Puppeteer, it downloads a recent version of Chromium (~170MB Mac, ~282MB Linux, ~280MB Win) that is guaranteed to work with the API. To skip the download, or to download a different browser, see [Environment variables](https://github.com/puppeteer/puppeteer/blob/v5.5.0/docs/api.md#environment-variables).
  28. ### puppeteer-core
  29. Since version 1.7.0 we publish the [`puppeteer-core`](https://www.npmjs.com/package/puppeteer-core) package,
  30. a version of Puppeteer that doesn't download any browser by default.
  31. ```bash
  32. npm i puppeteer-core
  33. # or "yarn add puppeteer-core"
  34. ```
  35. `puppeteer-core` is intended to be a lightweight version of Puppeteer for launching an existing browser installation or for connecting to a remote one. Be sure that the version of puppeteer-core you install is compatible with the
  36. browser you intend to connect to.
  37. See [puppeteer vs puppeteer-core](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#puppeteer-vs-puppeteer-core).
  38. ### Usage
  39. Puppeteer follows the latest [maintenance LTS](https://github.com/nodejs/Release#release-schedule) version of Node.
  40. Note: Prior to v1.18.1, Puppeteer required at least Node v6.4.0. Versions from v1.18.1 to v2.1.0 rely on
  41. Node 8.9.0+. Starting from v3.0.0 Puppeteer starts to rely on Node 10.18.1+. All examples below use async/await which is only supported in Node v7.6.0 or greater.
  42. Puppeteer will be familiar to people using other browser testing frameworks. You create an instance
  43. of `Browser`, open pages, and then manipulate them with [Puppeteer's API](https://github.com/puppeteer/puppeteer/blob/v5.5.0/docs/api.md#).
  44. **Example** - navigating to https://example.com and saving a screenshot as *example.png*:
  45. Save file as **example.js**
  46. ```js
  47. const puppeteer = require('puppeteer');
  48. (async () => {
  49. const browser = await puppeteer.launch();
  50. const page = await browser.newPage();
  51. await page.goto('https://example.com');
  52. await page.screenshot({path: 'example.png'});
  53. await browser.close();
  54. })();
  55. ```
  56. Execute script on the command line
  57. ```bash
  58. node example.js
  59. ```
  60. Puppeteer sets an initial page size to 800×600px, which defines the screenshot size. The page size can be customized with [`Page.setViewport()`](https://github.com/puppeteer/puppeteer/blob/v5.5.0/docs/api.md#pagesetviewportviewport).
  61. **Example** - create a PDF.
  62. Save file as **hn.js**
  63. ```js
  64. const puppeteer = require('puppeteer');
  65. (async () => {
  66. const browser = await puppeteer.launch();
  67. const page = await browser.newPage();
  68. await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle2'});
  69. await page.pdf({path: 'hn.pdf', format: 'A4'});
  70. await browser.close();
  71. })();
  72. ```
  73. Execute script on the command line
  74. ```bash
  75. node hn.js
  76. ```
  77. See [`Page.pdf()`](https://github.com/puppeteer/puppeteer/blob/v5.5.0/docs/api.md#pagepdfoptions) for more information about creating pdfs.
  78. **Example** - evaluate script in the context of the page
  79. Save file as **get-dimensions.js**
  80. ```js
  81. const puppeteer = require('puppeteer');
  82. (async () => {
  83. const browser = await puppeteer.launch();
  84. const page = await browser.newPage();
  85. await page.goto('https://example.com');
  86. // Get the "viewport" of the page, as reported by the page.
  87. const dimensions = await page.evaluate(() => {
  88. return {
  89. width: document.documentElement.clientWidth,
  90. height: document.documentElement.clientHeight,
  91. deviceScaleFactor: window.devicePixelRatio
  92. };
  93. });
  94. console.log('Dimensions:', dimensions);
  95. await browser.close();
  96. })();
  97. ```
  98. Execute script on the command line
  99. ```bash
  100. node get-dimensions.js
  101. ```
  102. See [`Page.evaluate()`](https://github.com/puppeteer/puppeteer/blob/v5.5.0/docs/api.md#pageevaluatepagefunction-args) for more information on `evaluate` and related methods like `evaluateOnNewDocument` and `exposeFunction`.
  103. <!-- [END getstarted] -->
  104. <!-- [START runtimesettings] -->
  105. ## Default runtime settings
  106. **1. Uses Headless mode**
  107. Puppeteer launches Chromium in [headless mode](https://developers.google.com/web/updates/2017/04/headless-chrome). To launch a full version of Chromium, set the [`headless` option](https://github.com/puppeteer/puppeteer/blob/v5.5.0/docs/api.md#puppeteerlaunchoptions) when launching a browser:
  108. ```js
  109. const browser = await puppeteer.launch({headless: false}); // default is true
  110. ```
  111. **2. Runs a bundled version of Chromium**
  112. By default, Puppeteer downloads and uses a specific version of Chromium so its API
  113. is guaranteed to work out of the box. To use Puppeteer with a different version of Chrome or Chromium,
  114. pass in the executable's path when creating a `Browser` instance:
  115. ```js
  116. const browser = await puppeteer.launch({executablePath: '/path/to/Chrome'});
  117. ```
  118. You can also use Puppeteer with Firefox Nightly (experimental support). See [`Puppeteer.launch()`](https://github.com/puppeteer/puppeteer/blob/v5.5.0/docs/api.md#puppeteerlaunchoptions) for more information.
  119. See [`this article`](https://www.howtogeek.com/202825/what%E2%80%99s-the-difference-between-chromium-and-chrome/) for a description of the differences between Chromium and Chrome. [`This article`](https://chromium.googlesource.com/chromium/src/+/master/docs/chromium_browser_vs_google_chrome.md) describes some differences for Linux users.
  120. **3. Creates a fresh user profile**
  121. Puppeteer creates its own browser user profile which it **cleans up on every run**.
  122. <!-- [END runtimesettings] -->
  123. ## Resources
  124. - [API Documentation](https://github.com/puppeteer/puppeteer/blob/v5.5.0/docs/api.md)
  125. - [Examples](https://github.com/puppeteer/puppeteer/tree/main/examples/)
  126. - [Community list of Puppeteer resources](https://github.com/transitive-bullshit/awesome-puppeteer)
  127. <!-- [START debugging] -->
  128. ## Debugging tips
  129. 1. Turn off headless mode - sometimes it's useful to see what the browser is
  130. displaying. Instead of launching in headless mode, launch a full version of
  131. the browser using `headless: false`:
  132. ```js
  133. const browser = await puppeteer.launch({headless: false});
  134. ```
  135. 2. Slow it down - the `slowMo` option slows down Puppeteer operations by the
  136. specified amount of milliseconds. It's another way to help see what's going on.
  137. ```js
  138. const browser = await puppeteer.launch({
  139. headless: false,
  140. slowMo: 250 // slow down by 250ms
  141. });
  142. ```
  143. 3. Capture console output - You can listen for the `console` event.
  144. This is also handy when debugging code in `page.evaluate()`:
  145. ```js
  146. page.on('console', msg => console.log('PAGE LOG:', msg.text()));
  147. await page.evaluate(() => console.log(`url is ${location.href}`));
  148. ```
  149. 4. Use debugger in application code browser
  150. There are two execution context: node.js that is running test code, and the browser
  151. running application code being tested. This lets you debug code in the
  152. application code browser; ie code inside `evaluate()`.
  153. - Use `{devtools: true}` when launching Puppeteer:
  154. ```js
  155. const browser = await puppeteer.launch({devtools: true});
  156. ```
  157. - Change default test timeout:
  158. jest: `jest.setTimeout(100000);`
  159. jasmine: `jasmine.DEFAULT_TIMEOUT_INTERVAL = 100000;`
  160. mocha: `this.timeout(100000);` (don't forget to change test to use [function and not '=>'](https://stackoverflow.com/a/23492442))
  161. - Add an evaluate statement with `debugger` inside / add `debugger` to an existing evaluate statement:
  162. ```js
  163. await page.evaluate(() => {debugger;});
  164. ```
  165. The test will now stop executing in the above evaluate statement, and chromium will stop in debug mode.
  166. 5. Use debugger in node.js
  167. This will let you debug test code. For example, you can step over `await page.click()` in the node.js script and see the click happen in the application code browser.
  168. Note that you won't be able to run `await page.click()` in
  169. DevTools console due to this [Chromium bug](https://bugs.chromium.org/p/chromium/issues/detail?id=833928). So if
  170. you want to try something out, you have to add it to your test file.
  171. - Add `debugger;` to your test, eg:
  172. ```js
  173. debugger;
  174. await page.click('a[target=_blank]');
  175. ```
  176. - Set `headless` to `false`
  177. - Run `node --inspect-brk`, eg `node --inspect-brk node_modules/.bin/jest tests`
  178. - In Chrome open `chrome://inspect/#devices` and click `inspect`
  179. - In the newly opened test browser, type `F8` to resume test execution
  180. - Now your `debugger` will be hit and you can debug in the test browser
  181. 6. Enable verbose logging - internal DevTools protocol traffic
  182. will be logged via the [`debug`](https://github.com/visionmedia/debug) module under the `puppeteer` namespace.
  183. # Basic verbose logging
  184. env DEBUG="puppeteer:*" node script.js
  185. # Protocol traffic can be rather noisy. This example filters out all Network domain messages
  186. env DEBUG="puppeteer:*" env DEBUG_COLORS=true node script.js 2>&1 | grep -v '"Network'
  187. 7. Debug your Puppeteer (node) code easily, using [ndb](https://github.com/GoogleChromeLabs/ndb)
  188. - `npm install -g ndb` (or even better, use [npx](https://github.com/zkat/npx)!)
  189. - add a `debugger` to your Puppeteer (node) code
  190. - add `ndb` (or `npx ndb`) before your test command. For example:
  191. `ndb jest` or `ndb mocha` (or `npx ndb jest` / `npx ndb mocha`)
  192. - debug your test inside chromium like a boss!
  193. <!-- [END debugging] -->
  194. <!-- [START typescript] -->
  195. ## Usage with TypeScript
  196. We have recently completed a migration to move the Puppeteer source code from JavaScript to TypeScript and we're currently working on shipping type definitions for TypeScript with Puppeteer's npm package.
  197. Until this work is complete we recommend installing the Puppeteer type definitions from the [DefinitelyTyped](https://definitelytyped.org/) repository:
  198. ```bash
  199. npm install --save-dev @types/puppeteer
  200. ```
  201. The types that you'll see appearing in the Puppeteer source code are based off the great work of those who have contributed to the `@types/puppeteer` package. We really appreciate the hard work those people put in to providing high quality TypeScript definitions for Puppeteer's users.
  202. <!-- [END typescript] -->
  203. ## Contributing to Puppeteer
  204. Check out [contributing guide](https://github.com/puppeteer/puppeteer/blob/main/CONTRIBUTING.md) to get an overview of Puppeteer development.
  205. <!-- [START faq] -->
  206. # FAQ
  207. #### Q: Who maintains Puppeteer?
  208. The Chrome DevTools team maintains the library, but we'd love your help and expertise on the project!
  209. See [Contributing](https://github.com/puppeteer/puppeteer/blob/main/CONTRIBUTING.md).
  210. #### Q: What is the status of cross-browser support?
  211. Official Firefox support is currently experimental. The ongoing collaboration with Mozilla aims to support common end-to-end testing use cases, for which developers expect cross-browser coverage. The Puppeteer team needs input from users to stabilize Firefox support and to bring missing APIs to our attention.
  212. From Puppeteer v2.1.0 onwards you can specify [`puppeteer.launch({product: 'firefox'})`](https://github.com/puppeteer/puppeteer/blob/v5.5.0/docs/api.md#puppeteerlaunchoptions) to run your Puppeteer scripts in Firefox Nightly, without any additional custom patches. While [an older experiment](https://www.npmjs.com/package/puppeteer-firefox) required a patched version of Firefox, [the current approach](https://wiki.mozilla.org/Remote) works with “stock” Firefox.
  213. We will continue to collaborate with other browser vendors to bring Puppeteer support to browsers such as Safari.
  214. This effort includes exploration of a standard for executing cross-browser commands (instead of relying on the non-standard DevTools Protocol used by Chrome).
  215. #### Q: What are Puppeteer’s goals and principles?
  216. The goals of the project are:
  217. - Provide a slim, canonical library that highlights the capabilities of the [DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/).
  218. - Provide a reference implementation for similar testing libraries. Eventually, these other frameworks could adopt Puppeteer as their foundational layer.
  219. - Grow the adoption of headless/automated browser testing.
  220. - Help dogfood new DevTools Protocol features...and catch bugs!
  221. - Learn more about the pain points of automated browser testing and help fill those gaps.
  222. We adapt [Chromium principles](https://www.chromium.org/developers/core-principles) to help us drive product decisions:
  223. - **Speed**: Puppeteer has almost zero performance overhead over an automated page.
  224. - **Security**: Puppeteer operates off-process with respect to Chromium, making it safe to automate potentially malicious pages.
  225. - **Stability**: Puppeteer should not be flaky and should not leak memory.
  226. - **Simplicity**: Puppeteer provides a high-level API that’s easy to use, understand, and debug.
  227. #### Q: Is Puppeteer replacing Selenium/WebDriver?
  228. **No**. Both projects are valuable for very different reasons:
  229. - Selenium/WebDriver focuses on cross-browser automation; its value proposition is a single standard API that works across all major browsers.
  230. - Puppeteer focuses on Chromium; its value proposition is richer functionality and higher reliability.
  231. That said, you **can** use Puppeteer to run tests against Chromium, e.g. using the community-driven [jest-puppeteer](https://github.com/smooth-code/jest-puppeteer). While this probably shouldn’t be your only testing solution, it does have a few good points compared to WebDriver:
  232. - Puppeteer requires zero setup and comes bundled with the Chromium version it works best with, making it [very easy to start with](https://github.com/puppeteer/puppeteer/#getting-started). At the end of the day, it’s better to have a few tests running chromium-only, than no tests at all.
  233. - Puppeteer has event-driven architecture, which removes a lot of potential flakiness. There’s no need for evil “sleep(1000)” calls in puppeteer scripts.
  234. - Puppeteer runs headless by default, which makes it fast to run. Puppeteer v1.5.0 also exposes browser contexts, making it possible to efficiently parallelize test execution.
  235. - Puppeteer shines when it comes to debugging: flip the “headless” bit to false, add “slowMo”, and you’ll see what the browser is doing. You can even open Chrome DevTools to inspect the test environment.
  236. #### Q: Why doesn’t Puppeteer v.XXX work with Chromium v.YYY?
  237. We see Puppeteer as an **indivisible entity** with Chromium. Each version of Puppeteer bundles a specific version of Chromium – **the only** version it is guaranteed to work with.
  238. This is not an artificial constraint: A lot of work on Puppeteer is actually taking place in the Chromium repository. Here’s a typical story:
  239. - A Puppeteer bug is reported: https://github.com/puppeteer/puppeteer/issues/2709
  240. - It turned out this is an issue with the DevTools protocol, so we’re fixing it in Chromium: https://chromium-review.googlesource.com/c/chromium/src/+/1102154
  241. - Once the upstream fix is landed, we roll updated Chromium into Puppeteer: https://github.com/puppeteer/puppeteer/pull/2769
  242. However, oftentimes it is desirable to use Puppeteer with the official Google Chrome rather than Chromium. For this to work, you should install a `puppeteer-core` version that corresponds to the Chrome version.
  243. For example, in order to drive Chrome 71 with puppeteer-core, use `chrome-71` npm tag:
  244. ```bash
  245. npm install puppeteer-core@chrome-71
  246. ```
  247. #### Q: Which Chromium version does Puppeteer use?
  248. Look for the `chromium` entry in [revisions.ts](https://github.com/puppeteer/puppeteer/blob/main/src/revisions.ts). To find the corresponding Chromium commit and version number, search for the revision prefixed by an `r` in [OmahaProxy](https://omahaproxy.appspot.com/)'s "Find Releases" section.
  249. #### Q: Which Firefox version does Puppeteer use?
  250. Since Firefox support is experimental, Puppeteer downloads the latest [Firefox Nightly](https://wiki.mozilla.org/Nightly) when the `PUPPETEER_PRODUCT` environment variable is set to `firefox`. That's also why the value of `firefox` in [revisions.ts](https://github.com/puppeteer/puppeteer/blob/main/src/revisions.ts) is `latest` -- Puppeteer isn't tied to a particular Firefox version.
  251. To fetch Firefox Nightly as part of Puppeteer installation:
  252. ```bash
  253. PUPPETEER_PRODUCT=firefox npm i puppeteer
  254. # or "yarn add puppeteer"
  255. ```
  256. #### Q: What’s considered a “Navigation”?
  257. From Puppeteer’s standpoint, **“navigation” is anything that changes a page’s URL**.
  258. Aside from regular navigation where the browser hits the network to fetch a new document from the web server, this includes [anchor navigations](https://www.w3.org/TR/html5/single-page.html#scroll-to-fragid) and [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API) usage.
  259. With this definition of “navigation,” **Puppeteer works seamlessly with single-page applications.**
  260. #### Q: What’s the difference between a “trusted" and "untrusted" input event?
  261. In browsers, input events could be divided into two big groups: trusted vs. untrusted.
  262. - **Trusted events**: events generated by users interacting with the page, e.g. using a mouse or keyboard.
  263. - **Untrusted event**: events generated by Web APIs, e.g. `document.createEvent` or `element.click()` methods.
  264. Websites can distinguish between these two groups:
  265. - using an [`Event.isTrusted`](https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted) event flag
  266. - sniffing for accompanying events. For example, every trusted `'click'` event is preceded by `'mousedown'` and `'mouseup'` events.
  267. For automation purposes it’s important to generate trusted events. **All input events generated with Puppeteer are trusted and fire proper accompanying events.** If, for some reason, one needs an untrusted event, it’s always possible to hop into a page context with `page.evaluate` and generate a fake event:
  268. ```js
  269. await page.evaluate(() => {
  270. document.querySelector('button[type=submit]').click();
  271. });
  272. ```
  273. #### Q: What features does Puppeteer not support?
  274. You may find that Puppeteer does not behave as expected when controlling pages that incorporate audio and video. (For example, [video playback/screenshots is likely to fail](https://github.com/puppeteer/puppeteer/issues/291).) There are two reasons for this:
  275. * Puppeteer is bundled with Chromium — not Chrome — and so by default, it inherits all of [Chromium's media-related limitations](https://www.chromium.org/audio-video). This means that Puppeteer does not support licensed formats such as AAC or H.264. (However, it is possible to force Puppeteer to use a separately-installed version Chrome instead of Chromium via the [`executablePath` option to `puppeteer.launch`](https://github.com/puppeteer/puppeteer/blob/v5.5.0/docs/api.md#puppeteerlaunchoptions). You should only use this configuration if you need an official release of Chrome that supports these media formats.)
  276. * Since Puppeteer (in all configurations) controls a desktop version of Chromium/Chrome, features that are only supported by the mobile version of Chrome are not supported. This means that Puppeteer [does not support HTTP Live Streaming (HLS)](https://caniuse.com/#feat=http-live-streaming).
  277. #### Q: I am having trouble installing / running Puppeteer in my test environment. Where should I look for help?
  278. We have a [troubleshooting](https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md) guide for various operating systems that lists the required dependencies.
  279. #### Q: How do I try/test a prerelease version of Puppeteer?
  280. You can check out this repo or install the latest prerelease from npm:
  281. ```bash
  282. npm i --save puppeteer@next
  283. ```
  284. Please note that prerelease may be unstable and contain bugs.
  285. #### Q: I have more questions! Where do I ask?
  286. There are many ways to get help on Puppeteer:
  287. - [bugtracker](https://github.com/puppeteer/puppeteer/issues)
  288. - [Stack Overflow](https://stackoverflow.com/questions/tagged/puppeteer)
  289. - [slack channel](https://join.slack.com/t/puppeteer/shared_invite/enQtMzU4MjIyMDA5NTM4LWI0YTE0MjM0NWQzYmE2MTRmNjM1ZTBkN2MxNmJmNTIwNTJjMmFhOWFjMGExMDViYjk2YjU2ZmYzMmE1NmExYzc)
  290. Make sure to search these channels before posting your question.
  291. <!-- [END faq] -->