Selenium:无法访问 iframe 和其中的数据 [英] Selenium: Unable to access iframe and data inside it

查看:42
本文介绍了Selenium:无法访问 iframe 和其中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 iframe,我有这样的 HTML 代码:

I have HTML code like this for an iframe:

<iframe class="class1" prio="0" title="Details" type="detail" source="/something/somepage.aspx" style="display:none" frameborder="0"></iframe>

这个框架有一个菜单、文本输入和按钮,因为它在当前页面上打开一个弹出窗口.Popup 从源页面获取数据,如上.

This frame has a menu, text input and buttons inside it as it opens a popup on the current page. Popup gets its data from the source page as above.

我尝试了不同的方法,即手动查找 iframe 的索引,然后显示其标题以查看我是否在正确的框架中,但没有运气.

I tried different approaches i.e. by finding index manually of the iframe and then display its title to see if I am at the right frame but had no luck with it.

我试图在 iframe 中的表单中键入数据,然后返回主页面,但我一无所知.请帮忙!

I am trying to type data in the form in iframe and then get back to the main page but am being clueless. Please help!

推荐答案

SwitchTo() 方法采用索引、名称或框架元素,因此您可以尝试使用名称或框架元素.

SwitchTo() method takes index, name or frame element, so you can try use name or frame element.

//find the frame by class/title/type/source
IWebElement detailFrame = driver.FindElement(By.XPath("//iframe[@class='class1']"));
driver.SwitchTo().Frame(detailFrame);

//alternatively, find the frame first, then use GetAttribute() to get frame name
IWebElement detailFrame = driver.FindElement(By.XPath("//iframe[@class='class1']"));
driver.SwitchTo().Frame(detailFrame.GetAttribute("name"));

//you are now in iframe "Details", then find the elements you want in the frame now
IWebElement foo = driver.FindElement(By.Id("foo"));
foo.Click();

//switch back to main frame
driver.SwitchTo().DefaultContent();

这篇关于Selenium:无法访问 iframe 和其中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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