0

I'm using selenium webdriver in java and I have lots of frames in the app under test.

here is my code for switching frames :

ClientPg.waitLong(1);
ClientPg.getWindowHandle();
ClientPg.switchToDefaultContent();


public void getWindowHandle() {

    for (String winHandle : driver.getWindowHandles()) {
        driver.switchTo().window(winHandle);
    }

    driver.manage().window().maximize();
        reportLog("Switch to new window. Title : " + driver.getTitle());
    }

    public void switchToDefaultContent() {
        driver.switchTo().defaultContent();
    }
}

with chrome it works as follows:

ClientPg.waitLong(1);
ClientPg.getWindowHandle();

but in firefox i need to add

:switchToDefaultContent();
Mrwan Ashraf
  • 41
  • 3
  • 10
user3313418
  • 35
  • 1
  • 8

1 Answers1

0

You can use following way to switch to particular I Frame which works fine.

For switch to perticular IFrame use the following function

public void switchframeByXpath(String xpath) throws Exception {

        try {
            System.out.println("switch to iframe");
            //wait for i frame element to load.
            WebDriver x = driver.switchTo().frame(driver.findElement(By.xpath(xpath)));
        } catch (Exception e) {
            throw new AssertionError("Can not Switch Iframe", e);
        }
    } 

For switch to main Frame use the following function

public void switchToMainFrame() {

        try {

            System.out.println("Before switching -- Switch to Main Frame");
            driver.switchTo().defaultContent();
            System.out.println("After switching -- Switch to Main Frame");
            Thread.sleep(1000);

        } catch (Exception e) {
            System.err.println("Can not Switch " + e.getMessage());
            throw new AssertionError("Switched to main Frame Error");
        }

    }
Jagrut
  • 96
  • 1
  • 8