2015-10-19 118 views
0

我正在尝试导航到iframe并使用硒返回到顶部帧。如何在Selenium中切换和切换

<html> 
    <body> 
     <div id="testID">This is upper frame</div> 
     <iframe src="test.html"></iframe> 
    </body> 
</html> 

我想获得testID,我很容易使用driver.findElement(By.id("testID"))

然后我想使用driver.switchTo().frame(driver.findElement(By.id("new_page"))); 导航到iframe,其中new_pagetest.htm里面的id。

现在,我的要求是切换回来,以便我可以再次从上述正文中读取testID

但是,如果使用: driver.switchTo().frame(driver.findElement(By.id("testID"))); 我得到的异常,如:

无法找到元素

回答

1

要切换到框架:

driver.switchTo().frame(0); 

要切换到主窗口bac K:

driver.switchTo().defaultContent(); 

另外我建议寻找帧的元件第一做开关,然后找到元件。

您可以通过以下3种方式切换到框架:

通过帧索引

按帧ID或名称

逐帧WebElement

了解更多详情请查看:Switch to Frame in Selenium

我希望它能帮到你..