クリック
要素をクリックします。
これは、選択された要素に対して WebDriver の `click` コマンドを発行します。通常、オプションが渡されない場合、選択された要素までスクロールしてクリックします。オプションオブジェクトが渡されると、webdriverのクリックではなくアクションクラスを使用します。これにより、ボタントタイプ、座標などの追加機能が提供されます。デフォルトでは、オプションを使用する場合、クリックアクションの実行後にリリースアクションコマンドが送信されます。このアクションをスキップするには、`option.skipRelease=true` を渡します。
注:固定ヘッダーやフッターなど、ビューポート内でスクロールされた後に選択された要素を覆い隠す固定位置要素がある場合、クリックは指定された座標で発行されますが、固定(オーバーレイ)要素によって受信されます。このような場合、次のエラーがスローされます。
Element is not clickable at point (x, x). Other element would receive the click: ..."
この問題を回避するには、オーバーレイ要素を見つけて、`execute` コマンドを使用して削除し、クリックを妨げないようにします。また、シナリオに適したオフセットを使用して、`scroll` を使用して要素に自分でスクロールすることもできます。
使用方法
$(selector).click({ button, x, y, skipRelease })
パラメータ
名前 | タイプ | 詳細 |
---|---|---|
オプション オプション | ClickOptions | クリックオプション(オプション) |
options.button | string 、number | [0, "left", 1, "middle", 2, "right"] のいずれかになります(オプション) |
options.x オプション | 数値 | 数値(オプション) |
options.y オプション | 数値 | 数値(オプション) |
options.skipRelease オプション | ブール値 | ブール値(オプション) |
例
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
})