page - puppeteerApp
In the Android app, you can run and call Kotlin code. Page By default, a page object has been instantiated, and you can use it directly. For example, await page.click('#id');
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>} , for example,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) ⇒ *
Get the specified element. If the element does not exist, return null. If there are multiple, return the first one.
Returns: *
- {Promise<any>}
Param | Type |
---|---|
selector | string |
Example
let ele = await page.$('#bkk');
let item = await page.$("=收藏按钮");
item.click();
console.log("item:" , JSON.stringify(item) );
page.$$(selector) ⇒ *
Get all specified elements, return an array.
Returns: *
- {Promise<any[]>}
Param | Type |
---|---|
selector | string |
Example
let eles = await page.$$('#bkk');
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.evaluate(pageFunction, ...args) ⇒ Promise.<Serializable>
Execute the specified method on the page. Similar to the eval() method.
Returns: Promise.<Serializable>
- The result of pageFunction execution.
Param | Type | Description |
---|---|---|
pageFunction | function | string | The method to be executed in the context of the page instance. |
...args | * | The parameters to be passed to pageFunction. |
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.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.screenshot([options]) ⇒ Promise.<string>
Take a screenshot of the current page.
Returns: Promise.<string>
- - A Promise object that resolves to the base64 string of the screenshot.
Param | Type | Default | Description |
---|---|---|---|
[options] | Object | {} | Optional configuration parameters: |
[options.path] | string | Screenshot save path. The file extension determines the image type. Relative paths are resolved from the current path. If no path is specified, it is not saved to the hard drive. | |
[options.type] | string | "'png'" | Specify the screenshot type, which can be 'jpeg' or 'png'. Default is 'png'. |
[options.quality] | number | Image quality (0-100), only applicable to jpeg type. | |
[options.clip] | Object | Specify a cropping area, | |
[options.clip.x] | number | Top left corner x coordinate of the cropping area. | |
[options.clip.y] | number | Top left corner y coordinate of the cropping area. | |
[options.clip.width] | number | Width of the cropping area. | |
[options.clip.height] | number | Height of the cropping area. | |
[options.omitBackground] | boolean | false | Whether to hide the default white background and make it transparent. Default is opaque. |
[options.encoding] | string | "'binary'" | Image encoding type, can be 'base64' or 'binary'. default 'binary'。 |
page.clicks(selectors, options)
Easy to click multiple elements. Such as inputting the amount, password.
Param | Type |
---|---|
selectors | * |
options | * |
Example
await page.clicks(['=1', '=.', '=8', '=8']);
Example
await page.clicks(['=1', '=.', '=8', '=8'], { delay: 200 });
Example
await page.clicks('1.88');
page.tap(x, y, [options]) ⇒ Promise.<void>
Perform a tap operation at the specified coordinate position.
Returns: Promise.<void>
- A Promise that resolves to indicate completion of the operation.
Param | Type | Default | Description |
---|---|---|---|
x | number | X-axis coordinate position | |
y | number | Y-axis coordinate position | |
[options] | Object | {} | Optional configuration parameters |
[options.clickCount] | number | 1 | Number of taps, default is 1 |
[options.delay] | number | 50 | Delay time between taps (milliseconds), default is 50 |
[options.interval] | number | 100 | Interval time between each tap (milliseconds), default is 100 |
[options.left] | number | null |
| Top left corner x coordinate of the cropping area.If left, top, width, and height are set, then coordinates will be randomly generated within that area. |
|
| [options.top] | number
| null
| | Top left corner y coordinate of the area. | | [options.width] |
number
| null
| | Width of the area. | | [options.height] |
number
| null
| | Height of the area. |
page.tapText(text, [options]) ⇒ Promise.<void>
Click on the specified text element. If multiple elements match, click on the first one. Address the issue of page.Click not working and element not found. Improve robustness. Wrap the following code into a method for easy use.
let elements = await page.elements()
let item = flatElements(elements).find(item => item.text === 'View leaderboard');
if(item){
let { left , top , right , bottom } = item.boundsInScreen ;
let x = ( left + right ) / 2 ;
let y = ( top + bottom ) / 2 ;
await page.tap( x , y );
}
Returns: Promise.<void>
- A Promise that resolves to indicate completion of the operation.
Param | Type | Default | Description |
---|---|---|---|
x | number | text,default>=selector。The text supports selector characters. For example:loving reading,=loving reading,or >=loving reading。 | |
[options] | Object | {} | Optional configuration parameters |
[options.clickCount] | number | 1 | Number of taps, default is 1 |
[options.delay] | number | 50 | Delay time between taps (milliseconds), default is 50 |
[options.interval] | number | 100 | Interval time between each tap (milliseconds), default is 100 |
page.elements
Get all elements on the current page, The data content is the nodeInfo converted to a JSON object.
Kind: static property of page
page.longTap ⇒ Promise.<void>
Perform a long tap operation at the specified coordinate position. Trigger long press, such as long pressing on an app icon on the phone system's home screen. The interface parameters are similar to page.tap.
Kind: static property of page
Returns: Promise.<void>
-A Promise that resolves to indicate completion of the operation.
Param | Type | Default | Description |
---|---|---|---|
x | number | X-axis coordinate position | |
y | number | Y-axis coordinate position | |
[options] | Object | {} | Optional configuration parameters: |
[options.delay] | number | 500 | Long press delay time (milliseconds), default is 500 |
[options.left] | number | null |
| Top left corner x coordinate of the area. |
[options.top] | number | null |
| Top left corner y coordinate of the area. |
[options.width] | number | null |
| Width of the area. |
[options.height] | number | null |
| Height of the area. |
page.touchDown ⇒ Promise.<void>
Simulate the touch operation of pressing down at the specified coordinate position.
Kind: static property of page
Returns: Promise.<void>
-A Promise that resolves to indicate completion of the operation.
Param | Type | Description |
---|---|---|
x | number | X-axis coordinate position |
y | number | Y-axis coordinate position |
page.touchUp ⇒ Promise.<void>
Simulate the touch operation of releasing the specified coordinate position.
Kind: static property of page
Returns: Promise.<void>
- A Promise that resolves to indicate completion of the operation.
Param | Type | Description |
---|---|---|
x | number | X-axis coordinate position |
y | number | Y-axis coordinate position |
page.touchMove ⇒ Promise.<void>
Simulate the touch operation of sliding from one coordinate position to another.
Kind: static property of page
Returns: Promise.<void>
- A Promise that resolves to indicate completion of the operation.
Param | Type | Default | Description |
---|---|---|---|
xFrom | number | start X-axis coordinate position | |
yFrom | number | start Y-axis coordinate position | |
xTo | number | target X-axis coordinate position | |
yTo | number | target Y-axis coordinate position | |
[duration] | number | 200 | Slide duration (milliseconds), default is 200 |
page.swipe ⇒ Promise.<void>
Simulate the slide operation of sliding from one coordinate position to another.
Kind: static property of page
Returns: Promise.<void>
- A Promise that resolves to indicate completion of the operation.
Param | Type | Default | Description |
---|---|---|---|
xFrom | number | start X-axis coordinate position | |
yFrom | number | start Y-axis coordinate position | |
xTo | number | target X-axis coordinate position | |
yTo | number | target Y-axis coordinate position | |
[duration] | number | 200 | Slide duration (milliseconds), default is 200 |
page.swipeDown ⇒ Promise.<void>
Simulate the operation of sliding down from the specified coordinate position.
Kind: static property of page
Returns: Promise.<void>
- A Promise that resolves to indicate completion of the operation.
Param | Type | Default | Description |
---|---|---|---|
x | number | X-axis coordinate position | |
y | number | Y-axis coordinate position | |
[duration] | number | 200 | Slide duration (milliseconds), default is 200 |
page.swipeUp ⇒ Promise.<void>
Simulate the operation of sliding up from the specified coordinate position.
Kind: static property of page
Returns: Promise.<void>
- A Promise that resolves to indicate completion of the operation.
Param | Type | Default | Description |
---|---|---|---|
x | number | X-axis coordinate position | |
y | number | Y-axis coordinate position | |
[duration] | number | 200 | Slide duration (milliseconds), default is 200 |
page.swipeLeft ⇒ Promise.<void>
Simulate the operation of sliding left from the specified coordinate position.
Kind: static property of page
Returns: Promise.<void>
- A Promise that resolves to indicate completion of the operation.
Param | Type | Default | Description |
---|---|---|---|
x | number | X-axis coordinate position | |
y | number | Y-axis coordinate position | |
[duration] | number | 200 | Slide duration (milliseconds), default is 200 |
page.swipeRight ⇒ Promise.<void>
Simulate the operation of sliding right from the specified coordinate position.
Kind: static property of page
Returns: Promise.<void>
- A Promise that resolves to indicate completion of the operation.
Param | Type | Default | Description |
---|---|---|---|
x | number | X-axis coordinate position | |
y | number | Y-axis coordinate position | |
[duration] | number | 200 | Slide duration (milliseconds), default is 200 |
page.home
Back to the App; default wait 200 milliseconds
Example
await page.home();