如何使用 Xpath 在 iframe 中选择元素? [英] How do I select elements inside an iframe with Xpath?
问题描述
我想创建一个 Selenium 测试来使用 AOL 邮件测试我们的扩展.我设法登录到 AOL 并撰写了一封电子邮件,但我还需要在 iframe 内的编辑器中选择元素.我检查过,即使打开编辑器,以下测试也失败了:
I want to create a Selenium test to test our extensions with AOL mail. I managed to login to AOL and compose an email, but I also need to select elements inside the editor, which is inside an iframe. I checked and even when the editor is open the following test fails:
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 以任何其他方式)选择 iframe 和其他元素的主体?
I get the error AssertionError: 1 != 0
. How do I select the body of the iframe and other elements by Xpath (or in any other way with Selenium)?
推荐答案
在切换到 之前,您无法遍历它们.你的 xPath,
You cannot traverse through <iframe>
's until switching to them. Your xPath,
//iframe[@name='editor_body']//body[@contenteditable='true']
将不起作用,因为 标记位于 iFrame 内,而该 iFrame 不在当前上下文中.你需要先切换到它:
will not work because the <body>
tag is within an iFrame, which is not in the current context. you need to switch to it first:
driver.switch_to.frame('editor_body')...
这篇关于如何使用 Xpath 在 iframe 中选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!