TEN*_*pin 5 typescript playwright
当我运行测试时,出现以下错误。我对剧作家和编码相当陌生,所以我不确定解决方案是什么
\nError: locator.click: Error: strict mode violation: getByRole(\'button\', { name: \'Statements\' }) resolved to 2 elements:\n1) <button title="Statements" aria-label="Statements" data-\xe2\x80\xa6>Statements</button> aka locator(\'#lp_line_6_a11y_queue\').getByRole(\'button\', { name: \'Statements\' })\n2) <button title="Statements" aria-label="Statements" data-\xe2\x80\xa6>Statements</button> aka locator(\'#lp_line_6\').getByRole(\'button\', { name: \'Statements\' })\nRun Code Online (Sandbox Code Playgroud)\n我刚刚进行了测试,希望不会出现错误。
\ntest(\'test\', async ({ page }) => {\n await page.goto(\'https://www.test.com\'); \n await page.getByPlaceholder(\'Section tag #1\').click();\n await page.getByPlaceholder(\'Section tag #1\').press(\'Control+a\');\n await page.getByPlaceholder(\'Section tag #1\').fill(\'inapptest\');\n\n await page.getByRole(\'button\', { name: \'UPDATE\' }).click();\n await page.getByRole(\'button\', { name: \'Message us\' }).click();\n await page.getByPlaceholder(\'Enter text here\').click();\n await page.getByPlaceholder(\'Enter text here\').fill(\'hi\');\n\n await page.getByPlaceholder(\'Enter text here\').press(\'Enter\'); \n await page.getByRole(\'button\', { name: \'Statements\' }).click();\n await page.getByRole(\'button\', { name: \'Get statements\' }).click();\n});`\nRun Code Online (Sandbox Code Playgroud)\n
定位器 API 是严格的,这意味着如果它在选择器中找到多个元素,并且您尝试调用定位器(例如 .click()),它就会抛出异常。
请参阅此处的严格性部分。我希望这有帮助。
https://playwright.dev/docs/locators#strictness https://playwright.dev/docs/api/class-locator
您可以通过locator.first()、locator.last()和locator.nth()告诉 Playwright 当多个元素匹配时使用哪个元素,从而显式选择退出严格性检查。
await page.getByRole('button', { name: 'Statements' }).first().click();
Run Code Online (Sandbox Code Playgroud)
注意:不建议使用这些方法,因为当您的页面发生更改时,Playwright 可能会单击您不想要的元素。相反,请遵循最佳实践来创建唯一标识目标元素的定位器。
为了使其独特,您可以通过文本进一步过滤它:
例子:
await page
.getByRole('listitem')
.filter({ hasText: 'Product 2' })
.getByRole('button', { name: 'Add to cart' })
.click();
Run Code Online (Sandbox Code Playgroud)
当然,如果您需要以及像表行数一样,您可以找到多个元素。
await page.locator('tr').first().waitFor();
console.log(await page.locator('tr').count());
Run Code Online (Sandbox Code Playgroud)
参考: https: //github.com/microsoft/playwright/issues/10611
| 归档时间: |
|
| 查看次数: |
6284 次 |
| 最近记录: |