Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Puppeteer #81

Open
iiuhuy opened this issue Feb 27, 2020 · 0 comments
Open

Puppeteer #81

iiuhuy opened this issue Feb 27, 2020 · 0 comments

Comments

@iiuhuy
Copy link
Owner

iiuhuy commented Feb 27, 2020

Puppeteer learning

主要是练习用于测试. 使用 Puppeteer 的 API.

配置 TypeScript 使用

tsc:
npm install -g typescript

tsconfig.json:

{
  "compilerOptions": {
    "target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
    "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "outDir": "out", /* Redirect output structure to the directory. */
    "rootDir": "src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    "strict": true, /* Enable all strict type-checking options. */
    "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
  }
}

使用:

// main.ts
import { Page, Browser, launch } from "puppeteer";

class Actions {
  /**
   * name
   */
  public async goUrl(page: Page, url: string) {
    await page.goto(url);
  }
}

async function main() {
  const action = new Actions();
  let browser: Browser = await launch({ headless: false });
  let page: Page = await browser.newPage();
  action.goUrl(page, "http://www.baidu.com");
}

main();

编译后运行 main.js 即可打开百度首页.

参考资料:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant