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

查看:52
本文介绍了硒:无法访问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>

此框架在内部打开菜单时显示菜单,文本输入和按钮。弹出窗口如上所述从源页面获取其数据。

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();

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

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