The Element module provides methods to control page elements in Mini Program.
Attributes
element.tagName
The tag name, in lowercase.
element.tagName: string
Methods
element.$
Find elements within the scope of a specific element.
element.$(selector: string): Promise<Element>
Parameters
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| selector | string | Yes | - | Selector |
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
let element = await page.$('.index-hd')
element = await element.$('.index-desc')
console.log(await element.text())
})
element.$$
Retrieves an array of elements within the scope of the element.
element.$$(selector: string): Promise<Element[]>
Parameters
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| selector | string | Yes | - | Selector |
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('.index-bd')
const elements = await element.$$('.kind-list-text')
console.log(await elements[0].text())
})
element.size
Get the size of an element.
element.size(): Promise<Object>
Return Value Description
| Field | Type | Description |
|---|---|---|
| width | number | Width of the element |
| height | number | Height of the element |
Sample code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('.index-bd')
const { width, height } = await element.size()
console.log(width, height)
})
element.offset
Get the absolute position of an element.
element.offset(): Promise<Object>
Return value description
| Field | Type | Description |
|---|---|---|
| left | number | X coordinate of the top-left corner, unit: px |
| top | number | Y coordinate of the top-left corner, unit: px |
Coordinate information uses the top-left corner of the page as the origin.
Sample code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('.index-bd')
const { left, top } = await element.offset()
console.log(left, top)
})
element.text
Retrieves the text of an element.
element.text(): Promise<string>
Code example:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('.index-desc')
console.log(await element.text())
})
element.attribute
Retrieves the attributes of an element.
element.attribute(name: string): Promise<string>
Parameters
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| name | string | Yes | - | Attribute name |
Sample code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('.index-logo')
console.log(await element.attribute('src')) // -> 'resources/kind/logo.png'
})
element.property
Supported from automator 0.9.0 and Base Library 2.9.5.
Gets the property of an element.
element.property(name: string): Promise<any>
Parameters
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| name | string | Yes | - | Property name |
The main differences between element.property and element.attribute are as follows:
- element.attribute retrieves the value from the tag, so its return type is always a string, whereas element.property is not necessarily so.
- element.attribute can retrieve values such as class and id, but element.property cannot.
- element.property can retrieve most property values listed for the corresponding component in the documentation, such as the value of form input components.
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('input')
console.log(await element.property('value'))
})
element.wxml
Get the WXML of the element.
element.wxml(): Promise<string>
element.outerWxml
Same as wxml, but it retrieves the element itself.
element.outerWxml(): Promise<string>
Sample code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('.index-desc')
console.log(await element.wxml())
console.log(await element.outerWxml())
})
element.value
Retrieves the value of the element.
element.value(): Promise<string>
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('.weui-input')
console.log(await element.value())
})
element.style
Retrieves the style value of an element.
element.style(name: string): Promise<string>
Parameter Description
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| name | string | Yes | - | Style name |
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('.index-desc')
console.log(await element.style('color')) // -> 'rgb(136, 136, 136)'
})
element.tap
Taps the element.
element.tap(): Promise<void>
Example:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('.kind-list-item-hd')
await element.tap()
})
element.longpress
Long press the element.
element.longpress(): Promise<void>
element.touchstart
Supported since automator 0.8.0 and Base Library 2.9.1.
Finger starts touching the element.
element.touchstart(options: Object): Promise<void>
The fields for options are defined as follows:
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| touches | array<Touch> | Yes | - | Touch event, an array of current touch points remaining on the screen |
| changedTouches | array<Touch> | Yes | - | Touch event, an array of current changed touch points |
element.touchmove
Supported since automator 0.8.0 and Base Library 2.9.1.
Move the finger after touching the element.
element.touchmove(options: Object): Promise<void>
The options fields are the same as those in touchstart.
element.touchend
Supported starting from automator 0.8.0 and Base Library 2.9.1.
The finger ends touching the element.
element.touchend(options: Object): Promise<void>
The fields in options are the same as for touchstart.
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('.touch')
await element.touchstart({
touches: [
{
identifier: 1,
pageX: 500,
pageY: 500
}
],
changedTouches: [
{
identifier: 1,
pageX: 500,
pageY: 500
}
]
})
await element.touchend({
touches: [],
changedTouches: [
{
identifier: 1,
pageX: 500,
pageY: 500
}
]
})
})
element.trigger
Triggers an element event.
element.trigger(type: string, detail?: Object): Promise<void>
Parameter Description
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| type | string | Yes | - | The type of event to trigger |
| detail | Object | No | - | The value of detail passed when triggering the event |
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('picker')
await element.trigger('change', { value: 1 })
})
This method cannot change the component state; it only triggers response methods. It also cannot trigger user interaction events such as tap, longpress, etc. Please use the corresponding methods for those actions.
element.input
Supported from automator 0.9.0 and Base Library 2.9.5.
Enter text. This method is only available for input and textarea components.
element.input(value: string): Promise<void>
Parameters
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| value | string | Yes | - | The text to be entered |
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('input')
await element.input('test')
})
element.callMethod
Supported since automator 0.6.0 and base library 2.9.0.
Invokes a specified method on a component instance. Only applicable to custom components.
element.callMethod(method: string, ...args: any[]): Promise<any>
Parameter Description
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| method | string | Yes | - | The name of the method to invoke |
| ...args | array<any> | No | - | Arguments for the method |
Code Example:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('set-tab-bar')
await element.callMethod('navigateBack')
})
element.data
Supported from automator 0.6.0 and Base Library 2.9.0 onwards.
Retrieves the rendered data of a component instance. This method is only available for custom components.
element.data(path?: string): Promise<Object>
Parameters
| Field | Type | Required | Default Value | Description |
|---|---|---|---|---|
| path | string | No | - | Data path |
Code example:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('set-tab-bar')
console.log(await element.data('hasSetTabBarBadge'))
})
element.setData
Supported from automator 0.6.0 and Base Library 2.9.0 onwards.
Sets the rendering data for a component instance. This method can only be used with custom components.
element.setData(data: Object): Promise<void>
Parameters
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| data | Object | Yes | - | The data to be modified |
Code example:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('set-tab-bar')
await element.setData({
hasSetTabBarBadge: true
})
})
element.callContextMethod
Supported since automator 0.9.0 and Base Library 2.9.5.
Calls a method on the Context object. This method is only available for the video component.
element.callContextMethod(method: string, ...args: any[]): Promise<any>
Parameter Description
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| method | string | Yes | - | The name of the method to be called |
| ...args | array<any> | No | - | Arguments for the method |
The video component must have an id set to use this feature.
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('video')
await element.callContextMethod('play')
})
element.scrollWidth
Supported from automator 0.9.0 and Base Library 2.9.5.
Gets the scroll width. Only available for the scroll-view component.
element.scrollWidth(): Promise<number>
element.scrollHeight
Supported from automator 0.9.0 and Base Library 2.9.5.
Gets the scroll height. This method is only available for the scroll-view component.
element.scrollHeight(): Promise<number>
element.scrollTo
Supported since automator 0.9.0 and Base Library 2.9.5.
Scrolls to a specified position. Only applicable to the scroll-view component.
element.scrollTo(x: number, y: number): Promise<void>
Parameters
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| x | number | Yes | - | Horizontal scroll position |
| y | number | Yes | - | Vertical scroll position |
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('scroll-view')
const y = (await element.scrollHeight()) - 50
await element.scrollTo(0, y)
})
element.swipeTo
Supported since automator 0.9.0 and Base Library 2.9.5.
Swipe to a specific slider. This method is only available for the swiper component.
element.swipeTo(index: number): Promise<void>
Parameter Description
| Field | Type | Required | Default Value | Description |
|---|---|---|---|---|
| index | number | Yes | - | The index of the target slider |
Sample code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('swiper')
await element.swipeTo(2)
})
element.moveTo
Supported since automator 0.9.0 and Base Library 2.9.5.
Moves the view container. Only the movable-view component can use this.
element.moveTo(x: number, y: number): Promise<void>
Parameter Description
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| x | number | Yes | - | Offset along the x-axis |
| y | number | Yes | - | Offset along the y-axis |
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('movable-view')
await element.moveTo(40, 40)
})
element.slideTo
Supported since automator 0.9.0 and Base Library 2.9.5.
Slides to the specified value. This method is only available for the slider component.
element.slideTo(value: number): Promise<void>
Parameters
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| value | number | Yes | - | The value to be set |
Sample code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('slider')
await element.slideTo(10)
})