Selenium无法切换到iframe

时间:2014-10-22 09:00:23

标签: iframe selenium webdriver automated-tests

我是Selenium的乞丐,但是从两天后我找不到解决方案。我在iframe中有代码,但我无法解决问题。 加载网站时,iframe会加入:

<div id="ext-gen98" class="x-panel-body x-panel-body-noheader">
            <iframe id="viewContent_iframe" class=" x-managed-iframe" width="100%" height="100%" src="/archibus/schema/ab-products/tests/alerts/ex-ekran-start.axvw" name="viewContent_iframe" style="border: medium none; height: 79px; width: 965px;">
                #document
                    <html id="ext-gen9" class=" x-viewport" webdriver="true">
                        <head></head>
                        <body id="ext-gen10" class="body ext-gecko x-border-layout-ct" margin="0 0 0 0" style="position: relative;">
                            <div id="ext-comp-1004" class="x-panel x-border-panel x-panel-noborder" style="left: 0px; top: 0px; width: 965px;">
                                <div id="ext-gen49" class="x-panel-bwrap">
                                    <div id="ext-gen50" class="x-panel-body x-panel-body-noheader x-panel-body-noborder" style="overflow: auto; width: 965px; height: 79px;"></div>
                                </div>
                            </div>
                            <div id="AFM_CALENDAR" style="position:absolute;display:none;background-color:white;layer-background-color:white;border:1px solid black;z-index:9999"></div>
                            <iframe id="AFM_CALENDAR_IFRAME" frameborder="0" src="javascript:false;" scrolling="no" style="position:absolute;top:0px;left:0px;display:none;"></iframe>
                            <div id="logger"></div>
                            <div id="viewToolbar_layoutWrapper"></div>
                            <div id="view_layoutWrapper"></div>
                        </body>
                    </html>
            </iframe>
        </div>

程序选择并点击链接:

<span id="task_4" class="leafnodes_text" style="background-image: url(&quot;http://architest:8080/archibus/schema/ab-system/graphics/ab-icon-tree-selected.gif&quot;);">Tasks</span> 

我知道在将这个新视图(* axvw)加载到iframe之后。

<iframe id="viewContent_iframe" class=" x-managed-iframe" width="100%" height="100%" src="/archibus/schema/ex_wymiar03.axvw" name="viewContent_iframe" style="border: medium none; height: 79px; width: 965px;">

#document
    <html class=" x-viewport" webdriver="true">
        <head></head>
        <body id="ext-gen9" class="body ext-gecko x-border-layout-ct" margin="0 0 0 0" style="position: relative;">
            <div id="ext-comp-1006" class="x-panel x-border-panel x-panel-noborder" style="left: 0px; top: 28px; width: 965px;">
                <div id="ext-gen91" class="x-panel-bwrap">
                    <div id="ext-gen92" class="x-panel-body x-panel-body-noheader x-panel-body-noborder x-border-layout-ct" style="width: 965px; height: 51px; position: relative;">
                        <div id="leftRegion" class="x-panel x-border-panel x-panel-noborder" style="width: 338px; left: 0px; top: 0px;">
                            <div id="ext-gen95" class="x-panel-bwrap">
                                <div id="ext-gen96" class="x-panel-body x-panel-body-noheader x-panel-body-noborder" style="width: 338px; height: 51px;">
                                    <div id="mainLayout_west_div">
                                        <div id="konfigPanel_layoutWrapper" class="">
                                            <div id="konfigPanel_head" class="panelToolbar"></div>
                                            <div id="konfigPanel" style="width: 100%; overflow: auto; height: 22px;"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div id="rightUpRegion" class="x-panel x-border-panel x-panel-noborder" style="left: 343px; top: 0px; width: 622px;"></div>
                        <div id="viewLayout_center_div"></div>
                        <div id="leftRegion-xsplit" class="x-layout-split x-layout-split-west x-splitbar-h" title="Drag to resize." style="left: 338px; top: 0px; height: 51px;"></div>
                    </div>
                </div>
            </div>

我尝试使用switchTo()以及我在网络中找到的所有内容。 例如,我需要获得使用css类的所有地方的数量: x-panel x-border-panel x-panel-noborder

我的代码:

公共课堂考试{

private WebDriver driver;
private WebDriverWait wait;
private String login = "login";
private String pass  = "pass";

@Before
public void setUp() throws Exception {      
    driver = new FirefoxDriver();   
}


  @Test
  public void testtestclass() throws Exception {
        driver.manage().window().maximize();
         //Open Home Page
         driver.get("http://architest:8080/archibus");
         Thread.sleep(2000);

         driver.findElement(By.id("username_input")).sendKeys(login);
         Thread.sleep(2000);

         driver.findElement(By.id("password_input")).sendKeys(pass);
         Thread.sleep(2000);

         driver.findElement(By.id("signin_button")).click();
         Thread.sleep(3000);

         driver.findElement(By.id("product_0")).click();
         Thread.sleep(2000);

         driver.findElement(By.id("activity_0")).click();
         Thread.sleep(2000);

         driver.findElement(By.id("process_2")).click();
         Thread.sleep(2000);

         driver.findElement(By.id("task_4")).click();
         Thread.sleep(2000);

         driver.switchTo().frame(driver.findElement(By.id("viewContent_iframe")));

         List <WebElement> x = driver.findElements(By.cssSelector("x-panel x-border-panel x-panel-noborder"));

         System.out.println("" + x.size());


         Thread.sleep(3000);
         driver.switchTo().defaultContent();
  }


  @After
  public void tearDown() throws Exception {
    driver.quit();
    }

}

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

切换到框架时,您不必使用驱动程序来查找元素。而不是这个:

driver.switchTo().frame(driver.findElement(By.id("viewContent_iframe")));

您可以使用该ID并执行此操作:

driver.switchTo().frame("viewContent_iframe");

另外,您是否尝试过搜索类名来查找所需的元素?类似的东西:

List <WebElement> x = driver.FindElements(By.ClassName("x-panel x-border-panel x-panel-noborder"))

祝你好运!

答案 1 :(得分:0)

  

By.cssSelector(“x-panel x-border-panel x-panel-noborder”)

好像你的定位器错了。它应该是

By.CssSelector(".x-panel .x-border-panel .x-panel-noborder")