如何使用Xpath选择iframe中的元素? [英] How do I select elements inside an iframe with Xpath?

查看:4125
本文介绍了如何使用Xpath选择iframe中的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个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)?

推荐答案

你在切换到它们之前,无法遍历< iframe> 。你的xPath,

You cannot traverse through <iframe>'s until switching to them. Your xPath,

//iframe[@name='editor_body']//body[@contenteditable='true']

无效,因为< body> tag位于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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆