2013-07-20 30 views

回答

0

您只能使用WebDriver在一帧内搜索。要与另一个框架中的元素进行交互,您需要“切换”它。然后司机会将其上下文转换到该框架中,并且您可以在其中工作。

一小块代码

driver.switchTo().frame() methods

大功告成后,使用

driver.switchTo().defaultContent(); 

此外,more documentation切换回主窗口。

0

在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()); 
    } 
} 
相关问题