Selenium:无法访问其中的iframe和数据

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

 

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

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

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

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

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

你试过(java代码):

 driver.switchTo().frame("iFrameName"); driver.findElement(By.id("formOne")).click(); driver.findElement(By.id("formOne")).sendKeys("abc");