Skip to content

page - puppeteerApp & chrome extension

The interface is the same as the Page class in Google's Puppeteer library.

page.title()

Get the name of the current app, such as WeChat.

Returns: * - {Promise<string>} `

page.url()

Get the package name of the current app, such as WeChat.

Returns: * - {Promise<string>} , 如com.tencent.mm/android.widget.FrameLayout

page.goto(activityFullName) ⇒ *

Jump to the activity of the specified app

Returns: * - {Promise<void>}

ParamType
activityFullNamestring

page.$(selector) ⇒ *

Returns the DOM element, or null if not found. It has the same function as document.querySelector. The object is cloned, not the original one. It is mainly used to get the attributes and content of DOM elements.

Returns: * - {Promise<any>}

ParamType
selectorstring

Example

js
let ele = await page.$('#bkk');

page.$$(selector) ⇒ *

Returns the DOM element, or null if not found. It has the same function as document.querySelector. The object is cloned, not the original one. It is mainly used to get the attributes and content of DOM elements.

Returns: * - {Promise<any[]>}

ParamType
selectorstring

Example

js
let eles = await page.$$('#bkk');

page.addScriptTag(options) ⇒ *

Add script, js will be added to the body.

Returns: * - {Promise<void>}

ParamTypeDescription
optionsObject
[options.url]stringthe URL of script
[options.content]stringthe content of script
[options.type]stringthe type of script , script.type = options.type;

Example

js
await page.addScriptTag({ url: 'https://code.jquery.com/jquery-3.2.1.min.js' });
await page.addScriptTag({ content: 'window.alert("Injected script")' });
await page.addScriptTag({
        content: 'window.alert("Injected script")',
        onload: 'console.log("load")'
});

page.addStyleTag(options) ⇒ *

Add style sheet, css will be added to the head.

Returns: * - {Promise<void>}

ParamTypeDescription
optionsObject
[options.url]stringURL of the style sheet
[options.content]stringcontent of the style sheet

Example

js
await page.addStyleTag({ url: 'https://fonts.googleapis.com/css?family=Roboto' });
await page.addStyleTag({ content: 'body { background-color: white; }' });
await page.addStyleTag({
        content: 'body { background-color: white; }',
        onload: 'console.log("load")'
});

page.click(selector, [options]) ⇒ *

Click on the specified element.

Returns: * - {Promise<void>}

ParamTypeDefault
selectorstring
[options]ClickOptions{ clickCount: 1, delay: 100 }

Example

js
await page.click('#bkk');

page.type(selector, text, [options]) ⇒ Promise.<void>

Input text in the specified element on the page.

Returns: Promise.<void> - Returns a Promise that resolves when the input operation is complete.

ParamTypeDefaultDescription
selectorstringThe element selector for the content to be input. If there are multiple matching elements, the input will be entered into the first matching element.
textstringThe text to be input.
[options]Object{ delay: 0, autoCorrect: true }Optional parameters.
[options.delay]number0The delay time between inputting texts (in milliseconds). Default is 0.
[options.autoCorrect]booleantrueSpecifies whether to automatically correct the input text. Default is true.

Example

js
await page.type('#bkk', 'test.844.ai');

page.waitForSelector(selector, options) ⇒ Promise.<AppElement>

The selector of the element to wait for. Waiting for the element matching the specified selector to appear on the page. If the matching element already exists when this method is called, it immediately returns. If the specified selector still does not appear after the timeout period, this method will throw an error.

Returns: Promise.<AppElement> - Returns a Promise that resolves to an AppElement object.

ParamTypeDescription
selectorstringThe selector of the element to wait for. It is the same as the parameter of document.querySelector(). css selector syntax. #id,.class,[attr=value],=text, >=text

| options | Object | Optional parameter: | | options.timeout | number | Timeout, unit milliseconds, default is 30000. | | options.pollingInterval | number | pollingInterval, unit milliseconds, default is 100. |

Example

js
await page.waitForSelector('#bkk');
await page.waitForSelector('#bkk', { timeout: 10000 });
await page.waitForSelector('.android.view.View[text="love reading"]')
await page.waitForSelector('.View[text="love reading"]');
let element = await page.waitForSelector('.View[text="love reading"]');
await element.click();

page.waitForFunction(pageFunction, [options], ...args) ⇒ *

Wait for the return value of pageFunction to be true. If it times out, an error will be thrown. waitForFunction can be used to monitor changes in the size of the page and screen rotation.

ParamTypeDefaultDescription
pageFunction*The method to be executed in the context of the page instance.
[options]Object{ timeout: 30000, pollingInterval: 100 }
...args*

Example

js
await page.waitForFunction(()=> Device.rotation() !=0 );

page.evaluate(fnOrString, ...args) ⇒ *

Execute the specified method in the page context and return the execution result. Wrap both synchronous and asynchronous methods once, and handle across devices. Reduce the logic code across devices. For example, WebViewInterceptor's webView.evaluateJavascript in Android app.

Returns: * - {Promise<any>} Returns a Promise that resolves with the result of executing fnOrString.

ParamTypeDescription
fnOrStringfunction | stringThe method to be executed in the page instance context
...argsanyThe parameters to be passed to fnOrString

Example

js
await page.evaluate(() => document.title);
await page.evaluate((a, b) => a + b, 1, 2);
await page.evaluate('document.title');
await page.evaluate('function() { return document.title }');
await page.evaluate(() => Promise.resolve(8 * 7));

page.home

Go back to the default page of the webview in the app; wait for 200 milliseconds by default.

Example

js
await page.home();