剧作家强制点击隐藏元素不起作用

Aug*_*Bar 4 javascript node.js playwright

我正在使用 Playwright 进行端到端测试。其中一种场景涉及检查 PDFviewer 窗口中显示的 pdf 内容,该窗口的下载按钮已对最终用户隐藏。检查 pdf 内容需要下载它,因此我使用\nforce文档提到的选项:\n https://playwright.dev/docs/api/class-page#page-click

\n

使用的实现如下:

\n
innerFrameContent.click("//button[contains(@id, \'secondaryDownload\')]", { force: true })\n
Run Code Online (Sandbox Code Playgroud)\n

(xpath是正确的,我在Chrome浏览器中检查并设法通过控制台单击该元素)

\n

不幸的是,我从 Playwright 收到以下异常日志:

\n
frame.click: Element is not visible\n=========================== logs ===========================\nwaiting for selector "//button[contains(@id, \'secondaryDownload\')]"\n  selector resolved to hidden <button tabindex="54" title="Download" id="secondaryDown\xe2\x80\xa6>\xe2\x80\xa6</button>\nattempting click action\n  waiting for element to be visible, enabled and stable\n    forcing action\n  element is visible, enabled and stable\n  scrolling into view if needed\n============================================================\n...\n
Run Code Online (Sandbox Code Playgroud)\n

har*_*ded 9

设置force为 true 意味着您想要绕过可操作性检查。这并不意味着它将click起作用。
在这种情况下,如果force设置为false,则操作将由于超时而失败,它将等待元素可见。
如果force设置为 true,则不会超时,但单击会失败,因为您无法单击不可见的元素。

您可能需要找到另一种方法来执行该操作,也许是 javascript?

  • 我做了类似的事情:`const unvisibleButton = wait browser.page.$(unvisibleButtonPath); unvisibleButton.evaluate((node: HTMLElement) =&gt; {node.click()})` (8认同)