能否请你解释一下如何用一小段代码处理Selenium WebDriver中的多个框架。如何处理硒web驱动程序中的多个框架?
回答
您只能使用WebDriver在一帧内搜索。要与另一个框架中的元素进行交互,您需要“切换”它。然后司机会将其上下文转换到该框架中,并且您可以在其中工作。
一小块代码
driver.switchTo().frame()
methods
大功告成后,使用
driver.switchTo().defaultContent();
此外,more documentation切换回主窗口。
在Selenium中使用iFrames,我们有不同的方法来根据需要处理框架。请看下面处理帧的方法
driver.switchTo().frame(int arg0);
通过其索引(从零开始)选择一个帧。也就是说,如果页面有多个帧(大于1),则第一帧将位于索引“0”,第二帧位于索引“1”,依此类推。
一旦框架被选中或导航,WebDriver接口上的所有后续调用都是针对该框架进行的。即驾驶员的重点将放在车架上。我们尝试在页面上执行的任何操作都不起作用,并在我们导航/切换到Frame时抛出元素。
参数索引 - (从零开始)索引 返回:驱动程序集中在给定帧(当前帧) 抛出:NoSuchFrameException - 如果未找到帧。例如:if iframe id = webklipper-publisher-widget-container-frame,它可以写成driver.switchTo().frame(“webklipper-publisher-widget-container-frame”);以下是使用frame id与switchToFrame配合使用的代码片段。
public void switchToFrame(int frame) {
try {
driver.switchTo().frame(frame);
System.out.println("Navigated to frame with id " + frame);
} catch (NoSuchFrameException e) {
System.out.println("Unable to locate frame with id " + frame
+ e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to navigate to frame with id " + frame
+ e.getStackTrace());
}
}driver.switchTo().frame(String arg0);
通过其名称或ID选择一个框架。通过匹配名称属性定位的框架总是优先于通过ID匹配的框架。 参数:name或Id - 框架的名称或框架元素的ID。 返回:驱动程序集中在给定帧(当前帧) 抛出:NoSuchFrameException - 如果未找到帧
下面是使用帧名称的示例代码片段。
public void switchToFrame(String frame){ 尝试driver.switchTo()。frame(frame); System.out.println(“导航到名称框架”+框架); System.out.println(“无法找到带有ID的帧”+帧 + e.getStackTrace()); System.out.println(“无法导航到带有ID的帧”+帧 + e.getStackTrace()); } } driver.switchTo()。框架(WebElement frameElement);
使用之前定位的WebElement选择一个框架。 参数:frameElement - 要切换到的框架元素。 返回:驱动程序关注给定帧(当前帧)。 抛出:NoSuchFrameException - 如果给定的元素既不是iframe也不是框架元素。和StaleElementReferenceException - 如果WebElement已过时。
下面是将元素发送到和开关的示例代码。
public void switchToFrame(WebElement frameElement)
{
try
{
if (isElementPresent(frameElement))
driver.switchTo().frame(frameElement);
System.out.println("Navigated to frame with element "+ frameElement);
} else {
System.out.println("Unable to navigate to frame with element "+ frameElement);
}
} catch (NoSuchFrameException e) {
System.out.println("Unable to locate frame with element " + frameElement + e.getStackTrace());
} catch (StaleElementReferenceException e) {
System.out.println("Element with " + frameElement + "is not attached to the page document" + e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to navigate to frame with element " + frameElement + e.getStackTrace());
}
}
当有多个帧(在A面框镜架)有些时候,我们需要先切换到父框架,然后我们需要切换到子框架。下面是使用多个框架的代码片段。
public void switchToFrame(String ParentFrame, String ChildFrame) {
try {
driver.switchTo().frame(ParentFrame).switchTo().frame(ChildFrame);
System.out.println("Navigated to innerframe with id " + ChildFrame
+ "which is present on frame with id" + ParentFrame);
} catch (NoSuchFrameException e) {
System.out.println("Unable to locate frame with id " + ParentFrame
+ " or " + ChildFrame + e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to navigate to innerframe with id "
+ ChildFrame + "which is present on frame with id"
+ ParentFrame + e.getStackTrace());
}
}
使用框架后,主要重要的是要回到网页。如果我们不切换回默认页面,驱动程序会抛出异常。以下是切换回默认内容的代码片段。
public void switchtoDefaultFrame() {
try {
driver.switchTo().defaultContent();
System.out.println("Navigated back to webpage from frame");
} catch (Exception e) {
System.out
.println("unable to navigate back to main webpage from frame"
+ e.getStackTrace());
}
}
- 1. 如何使用硒web驱动程序处理Flex网格?
- 2. 如何使用硒web驱动程序处理Windows下载对话框
- 3. 如何点击一个复选框使用Java的硒web驱动程序?
- 4. 如何在python中保存硒web驱动程序的状态?
- 5. 保护硒web驱动程序交互
- 6. Rails 3.2硒web驱动程序测试
- 7. Selenium Web驱动程序和硒RC
- 8. 硒Web驱动程序单击
- 9. 硒远程Web驱动程序:如何开始从客户
- 10. 如何通过使用框架来处理Selenium Web驱动程序TestNg中的子窗口?
- 11. 如何使用Selenium Web驱动程序处理Flash对象
- 12. 如何使用硒web驱动程序获取gwt combobox的值?
- 13. 如何整合中硒的webdriver关键字驱动框架
- 14. 如何在多个驱动程序中处理Verilog中的总线
- 15. C#硒web驱动程序如何做while循环元素
- 16. 如何使用硒web驱动程序控制“Ajax”?
- 17. Selenium Node API Web驱动程序等待超时处理程序
- 18. Python中硒的网络驱动器多处理
- 19. 在RIDE(机器人框架)中添加chrome web驱动程序
- 20. 使用java的硒web驱动程序基本程序
- 21. 硒2铬驱动程序
- 22. 如何处理使用Web驱动程序动态生成值的表?
- 23. 在多个Python脚本中保留硒驱动程序
- 24. 需要xpath用于选择硒web驱动程序中的复选框
- 25. 处理两个JDBC驱动程序的通用JDBC驱动程序
- 26. 高效滚动使用硒的Web驱动程序
- 27. 如何在Web应用程序中处理多个弹出对话框
- 28. 管理多个框架应用程序
- 29. 如何处理USB过滤器驱动程序中的IRP_MN_QUERY_DEVICE_RELATIONS
- 30. 使用硒web驱动程序在多个浏览器上运行测试