Python 如何使用 Xpath 在 iframe 中选择元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28814916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I select elements inside an iframe with Xpath?
提问by Uri
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:
我想创建一个 Selenium 测试来使用 AOL 邮件测试我们的扩展。我设法登录到 AOL 并撰写了一封电子邮件,但我还需要在 iframe 内的编辑器中选择元素。我检查过,即使编辑器打开,以下测试也失败:
self.assertEqual(first=1, second=len(self.driver.find_elements_by_xpath(xpath="//iframe[@name='editor_body']//body[@contenteditable='true']")))
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)?
我收到错误AssertionError: 1 != 0
。如何通过 Xpath(或以任何其他方式使用 Selenium)选择 iframe 和其他元素的主体?
采纳答案by ddavison
You cannot traverse through <iframe>
's until switching to them. Your xPath,
<iframe>
在切换到它们之前,您无法遍历's。你的 xPath,
//iframe[@name='editor_body']//body[@contenteditable='true']
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:
将不起作用,因为<body>
标记位于 iFrame 中,而 iFrame 不在当前上下文中。你需要先切换到它:
driver.switch_to.frame('editor_body')...