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>}
Param | Type |
---|---|
activityFullName | string |
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>}
Param | Type |
---|---|
selector | string |
Example
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[]>}
Param | Type |
---|---|
selector | string |
Example
let eles = await page.$$('#bkk');
page.addScriptTag(options) ⇒ *
Add script, js will be added to the body.
Returns: *
- {Promise<void>}
Param | Type | Description |
---|---|---|
options | Object | |
[options.url] | string | the URL of script |
[options.content] | string | the content of script |
[options.type] | string | the type of script , script.type = options.type; |
Example
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>}
Param | Type | Description |
---|---|---|
options | Object | |
[options.url] | string | URL of the style sheet |
[options.content] | string | content of the style sheet |
Example
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>}
Param | Type | Default |
---|---|---|
selector | string | |
[options] | ClickOptions | { clickCount: 1, delay: 100 } |
Example
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.
Param | Type | Default | Description |
---|---|---|---|
selector | string | The element selector for the content to be input. If there are multiple matching elements, the input will be entered into the first matching element. | |
text | string | The text to be input. | |
[options] | Object | { delay: 0, autoCorrect: true } | Optional parameters. |
[options.delay] | number | 0 | The delay time between inputting texts (in milliseconds). Default is 0. |
[options.autoCorrect] | boolean | true | Specifies whether to automatically correct the input text. Default is true. |
Example
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.
Param | Type | Description |
---|---|---|
selector | string | The 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
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.
Param | Type | Default | Description |
---|---|---|---|
pageFunction | * | The method to be executed in the context of the page instance. | |
[options] | Object | { timeout: 30000, pollingInterval: 100 } | |
...args | * |
Example
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.
Param | Type | Description |
---|---|---|
fnOrString | function | string | The method to be executed in the page instance context |
...args | any | The parameters to be passed to fnOrString |
Example
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
await page.home();