انتقل إلى المحتوى الرئيسي

النقر

النقر على عنصر.

يصدر هذا أمر 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"]
للويب فقط (سطح المكتب/الجوال)
options.x
اختياري
numberينقر على بعد X بكسل أفقيًا من موقع العنصر (من نقطة مركز العنصر)
الويب والتطبيقات الأصلية (سطح المكتب/الجوال)
options.y
اختياري
numberينقر على بعد Y بكسل عموديًا من موقع العنصر (من نقطة مركز العنصر)
دعم الويب والتطبيقات الأصلية (سطح المكتب/الجوال)
options.skipRelease
اختياري
booleanقيمة منطقية (اختياري)
للويب فقط (سطح المكتب/الجوال)
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 })
})

Welcome! How can I help?

WebdriverIO AI Copilot