我正在尝试使用 Python 和 Playwright 在 iframe 中选择一个按钮...在 Selenium 中我知道你可以通过使用索引来做到这一点,这在 playwright 中是否可能,我一直在挖掘文档,但似乎无法想办法。我尝试选择的 iframe 中包含的按钮是:
"button:has-text(\"Add New User\")"
我使用的 iframe 的 html 代码看起来与此类似:
<iframe src="https://www.urlthatcannotbereturnedinpagehtml.com/veryparticularparameters" width="100%" style="height: 590px;"></iframe>
有人有什么想法吗?关于这里束手无策......我试图通过解析网页的代码来找到url,但是这部分不能像那样选择。我可能只是对剧作家的文档感到不知所措,我在硒上花了很多时间,这似乎是一种全新的语言。
谢谢!
据我了解,您的页面在 iframe 中包含内容。您想要使用 Playwright 访问该框架内的元素。
处理帧的官方文档:官方文档:https://playwright.dev/docs/frames#frame-objects
然后你可以尝试这样的事情:
// Locate element inside frame
const iframeButton = await page.frameLocator('iFrame').locator("button:has-text(\"Add New User\")");
await iframeButton.click();
请注意,该示例使用 iFrame 标记作为定位器,但您可以而且应该使用更准确的内容,例如 id、名称或 url。