クリック
要素をクリックします。
これは選択した要素に対してWebDriverのclick
コマンドを発行します。オプションが渡されていない場合は、一般的に選択した要素までスクロールしてからクリックします。オプションオブジェクトが渡された場合は、webdriverのクリックの代わりにアクションクラスを使用し、ボタンタイプや座標などの追加機能を利用できます。デフォルトでは、オプションを使用する場合、クリックアクション後にリリースアクションコマンドが送信されますが、option.skipRelease=true
を渡すとこのアクションをスキップできます。
情報
固定位置の要素(固定ヘッダーやフッターなど)があり、選択した要素がビューポート内にスクロールされた後にその要素を覆ってしまう場合、クリックは指定された座標で発行されますが、固定(重なっている)要素によって受け取られます。このような場合、次のエラーが発生します:
Element is not clickable at point (x, x). Other element would receive the click: ..."
この問題を回避するには、重なっている要素を見つけてexecute
コマンドで削除し、クリックを妨げないようにしてください。また、あなたのシナリオに適したオフセットを使用してscroll
で要素まで自分でスクロールすることもできます。
情報
クリックコマンドは、モバイルデバイスでの長押しをシミュレートするためにも使用できます。これはduration
を設定することで行います。詳細については以下の例を参照してください。
使用方法
$(selector).click({ button, x, y, skipRelease, duration })
パラメータ
名前 | タイプ | 詳細 |
---|---|---|
options オプション | ClickOptions | クリックオプション(オプション) |
options.button オプション | string, number | [0, "left", 1, "middle", 2, "right"] のいずれか WEBのみ(デスクトップ/モバイル) |
options.x オプション | number | 要素の位置(要素の中心点)から水平方向にX ピクセル離れた位置をクリック WEBおよびネイティブ(デスクトップ/モバイル) |
options.y オプション | number | 要素の位置(要素の中心点)から垂直方向にY ピクセル離れた位置をクリック WEBおよびネイティブ対応(デスクトップ/モバイル) |
options.skipRelease オプション | boolean | ブール値(オプション) WEBのみ(デスクトップ/モバイル) |
options.duration オプション | number | クリックの持続時間、「長押し」 モバイルネイティブアプリのみ(モバイル) |
例
example.html
<button id="myButton" onclick="document.getElementById('someText').innerHTML='I was clicked'">Click me</button>
<div id="someText">I was not clicked</div>
click.js
it('should demonstrate the click command', async () => {
const myButton = await $('#myButton')
await myButton.click()
const myText = await $('#someText')
const text = await myText.getText()
assert(text === 'I was clicked') // true
})
example.js
it('should fetch menu links and visit each page', async () => {
const links = await $$('#menu a')
await links.forEach(async (link) => {
await link.click()
})
})
example.html
<button id="myButton">Click me</button>
example.js
it('should demonstrate a click using an offset', async () => {
const myButton = await $('#myButton')
await myButton.click({ x: 30 }) // clicks 30 horizontal pixels away from location of the button (from center point of element)
})
example.html
<button id="myButton">Click me</button>
example.js
it('should demonstrate a right click passed as string', async () => {
const myButton = await $('#myButton')
await myButton.click({ button: 'right' }) // opens the contextmenu at the location of the button
})
it('should demonstrate a right click passed as number while adding an offset', async () => {
const myButton = await $('#myButton')
await myButton.click({ button: 2, x: 30, y: 40 }) // opens the contextmenu 30 horizontal and 40 vertical pixels away from location of the button (from the center of element)
})
it('should skip sending releaseAction command that cause unexpected alert closure', async () => {
const myButton = await $('#myButton')
await myButton.click({ button: 2, x: 30, y: 40, skipRelease:true }) // skips sending releaseActions
})
longpress.example.js
it('should be able to open the contacts menu on iOS by executing a longPress', async () => {
const contacts = await $('~Contacts')
// opens the Contacts menu on iOS where you can quickly create
// a new contact, edit your home screen, or remove the app
await contacts.click({ duration: 2000 })
})