如何使用 Xpath 在 iframe 中 select 元素?
How do I select elements inside an iframe with Xpath?
我想创建一个 Selenium 测试来测试我们使用 AOL 邮件的扩展。我设法登录到 AOL 并撰写了一封电子邮件,但我还需要 select 编辑器中的元素,它位于 iframe 中。我检查过,即使打开编辑器,以下测试也失败了:
self.assertEqual(first=1, second=len(self.driver.find_elements_by_xpath(xpath="//iframe[@name='editor_body']//body[@contenteditable='true']")))
我收到错误 AssertionError: 1 != 0
。我如何通过 Xpath(或以任何其他方式使用 Selenium)select iframe 的主体和其他元素?
在切换到 <iframe>
之前,您无法遍历它们。你的 xPath,
//iframe[@name='editor_body']//body[@contenteditable='true']
将不起作用,因为 <body>
标记位于 iFrame 中,而 iFrame 不在当前上下文中。你需要先切换到它:
driver.switch_to.frame('editor_body')...
我想创建一个 Selenium 测试来测试我们使用 AOL 邮件的扩展。我设法登录到 AOL 并撰写了一封电子邮件,但我还需要 select 编辑器中的元素,它位于 iframe 中。我检查过,即使打开编辑器,以下测试也失败了:
self.assertEqual(first=1, second=len(self.driver.find_elements_by_xpath(xpath="//iframe[@name='editor_body']//body[@contenteditable='true']")))
我收到错误 AssertionError: 1 != 0
。我如何通过 Xpath(或以任何其他方式使用 Selenium)select iframe 的主体和其他元素?
在切换到 <iframe>
之前,您无法遍历它们。你的 xPath,
//iframe[@name='editor_body']//body[@contenteditable='true']
将不起作用,因为 <body>
标记位于 iFrame 中,而 iFrame 不在当前上下文中。你需要先切换到它:
driver.switch_to.frame('editor_body')...