Arkadiusz Zięba
Arkadiusz Zięba

Reputation: 23

Selenium cannot switch to iframe

I am begginer in Selenium, but from two days i can't find a solution. I have code in iframe and i can't get to it. When website is loaded, iframe cointains:

<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>

Program selects and clicks on the link:

<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> 

I know that after this new view(*axvw) is loaded to 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>

I try switchTo() and everything what I found in network. For example I need to get number of all places where there are used css class: x-panel x-border-panel x-panel-noborder

My code:

public class test {

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

}

Thank you for your help

Upvotes: 1

Views: 1546

Answers (2)

Shmoman
Shmoman

Reputation: 139

When you switch to the frame you don't have to use the driver to find the element. instead of this:

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

you can just use the ID and do this:

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

Also, have you tried just searching the class names to find the elements you need? Something like:

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

Good Luck!

Upvotes: 1

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

Seems like you locators are wrong. It should be

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

Upvotes: 0

Related Questions