Vai al contenuto principale

click

Fai clic su un elemento.

Questo emette un comando WebDriver click per l'elemento selezionato, che generalmente scorre fino all'elemento selezionato e poi ci clicca sopra quando non vengono passate opzioni. Quando viene passato l'oggetto options, utilizza la classe action invece del click webdriver, che offre funzionalità aggiuntive come la possibilità di passare il tipo di pulsante, le coordinate, ecc. Per impostazione predefinita, quando si utilizzano le opzioni, viene inviato un comando di rilascio dopo aver eseguito l'azione di clic; passa option.skipRelease=true per saltare questa azione.

informazione

Se hai elementi in posizione fissa (come un'intestazione o un piè di pagina fissi) che coprono l'elemento selezionato dopo che è stato scorruto all'interno della viewport, il clic verrà emesso alle coordinate specificate, ma sarà ricevuto dal tuo elemento fisso (sovrapposto). In questi casi viene generato il seguente errore:

Element is not clickable at point (x, x). Other element would receive the click: ..."

Per aggirare questo problema, prova a trovare l'elemento sovrapposto e rimuoverlo tramite il comando execute in modo che non interferisca con il clic. Puoi anche provare a scorrere fino all'elemento da solo usando scroll con un offset appropriato per il tuo scenario.

informazione

Il comando click può anche essere utilizzato per simulare una pressione prolungata su un dispositivo mobile. Questo si fa impostando il parametro duration. Vedi l'esempio qui sotto per maggiori informazioni.

Utilizzo
$(selector).click({ button, x, y, skipRelease, duration })
Parametri
NomeTipoDettagli
options
opzionale
ClickOptionsOpzioni di clic (opzionale)
options.button
opzionale
string, numberPuò essere uno di [0, "left", 1, "middle", 2, "right"]
SOLO WEB (Desktop/Mobile)
options.x
opzionale
numberFa clic a X pixel orizzontali di distanza dalla posizione dell'elemento (dal punto centrale dell'elemento)
WEB e Native (Desktop/Mobile)
options.y
opzionale
numberFa clic a Y pixel verticali di distanza dalla posizione dell'elemento (dal punto centrale dell'elemento)
Supporto WEB e Native (Desktop/Mobile)
options.skipRelease
opzionale
booleanBooleano (opzionale)
SOLO WEB (Desktop/Mobile)
options.duration
opzionale
numberDurata del clic, anche noto come "LongPress"
SOLO APP MOBILE NATIVE (Mobile)
Esempi
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 })
})

Welcome! How can I help?

WebdriverIO AI Copilot