38

I am using java with WebDriver.I have to switch between two frames. I have recorded the test case in selenium IDE and in that I got the values as selectFrame relative=top select Frame=middle Frame

But there is a problem it is not able to recognize the relative=top and middleFrame. How can I solve this problem in Selenium WebDriver with Java?

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Mallik
  • 679
  • 2
  • 14
  • 18

7 Answers7

58

WebDriver's driver.switchTo().frame() method takes one of the three possible arguments:

  • A number.

    Select a frame by its (zero-based) index. That is, if a page has three frames, the first frame would be at index 0, the second at index 1 and the third at index 2. Once the frame has been selected, all subsequent calls on the WebDriver interface are made to that frame.

  • A name or ID.

    Select a frame by its name or ID. Frames located by matching name attributes are always given precedence over those matched by ID.

  • A previously found WebElement.

    Select a frame using its previously located WebElement.

Get the frame by it's id/name or locate it by driver.findElement() and you'll be good.

secretformula
  • 6,414
  • 3
  • 33
  • 56
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • How does switchTo().frame(0) handle iframe VS. frame-in-frameset? Is the "frameset" tag counted? – djangofan Jan 14 '14 at 23:27
  • @djangofan Good question. I don't know, I haven't ever used the number parameter. I'd guess it doesn't count the `` element. I you ever test it, please share your results :). – Petr Janeček Jan 15 '14 at 10:47
  • 3
    Even though this is old I will post for others, from my experience it does **not** count `frameset` elements. – aurbano Mar 26 '14 at 09:06
  • 3
    @aurbano - just want you to know that 3 years later, your comment fixed the issue I was having! THANK YOU!!! – FreeMan May 11 '17 at 14:11
  • I think currently maybe a different method - `SwitchToFrame()` - needs to be used. More info [here](https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/remote/server/handler/SwitchToFrame.html) probably. It would be nice if someone could update this answer. I'm in JS myself, but I don't like broken links. – Carolus Oct 11 '19 at 11:37
17

to switchto a frame:

driver.switchTo.frame("Frame_ID");

to switch to the default again.

driver.switchTo().defaultContent();
user1710861
  • 413
  • 6
  • 9
  • 2
    This is tricky. Switching to the default content is one of the worst methods I know in Seleniums API. Since if you go for driver.switchTo().defaultContent() and again issue driver.switchTo().defaultContent() there is no guarantee where you end up. The Java Doc states: Selects either the first frame on the page, or the main document when a page contains iframes. So if you are already in the root frame you end up in the first frame with all the chaos this brings. So always test if you are already in the root frame (javascript -> return self.location === top.location;) – Martin Kersten Jan 10 '15 at 10:47
6

First you have to locate the frame id and define it in a WebElement

For ex:- WebElement fr = driver.findElementById("id");

Then switch to the frame using this code:- driver.switchTo().frame("Frame_ID");

An example script:-

WebElement fr = driver.findElementById("theIframe");

driver.switchTo().frame(fr);

Then to move out of frame use:- driver.switchTo().defaultContent();
Irshad
  • 3,071
  • 5
  • 30
  • 51
ArunPaul
  • 61
  • 1
  • 1
3

You can also use:

driver.switch_to.frame(0)

(0) being the first iframe on the html.

to switch back to the default content:

driver.switch_to.default_content()
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
2

Need to make sure once switched into a frame, need to switch back to default content for accessing webelements in another frames. As Webdriver tend to find the new frame inside the current frame.

driver.switchTo().defaultContent()
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Krish13287
  • 198
  • 1
  • 1
  • 10
2

There is also possibility to use WebDriverWait with ExpectedConditions (to make sure that Frame will be available).

  1. With string as parameter

    (new WebDriverWait(driver, 5)).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("frame-name"));
    
  2. With locator as a parameter

    (new WebDriverWait(driver, 5)).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("frame-id")));
    

More info can be found here

rkarczmarczyk
  • 327
  • 5
  • 13
1

This code is in groovy, so most likely you will need to do some rework. The first param is a url, the second is a counter to limit the tries.

public boolean selectWindow(window, maxTries) {
    def handles
    int tries = 0
    while (true) {
        try {
            handles = driver.getWindowHandles().toArray()
            for (int a = handles.size() - 1; a >= 0 ; a--) { // Backwards is faster with FF since it requires two windows
                try {
                    Log.logger.info("Attempting to select window: " + window)
                    driver.switchTo().window(handles[a]);
                    if (driver.getCurrentUrl().equals(window))
                        return true;
                    else {
                        Thread.sleep(2000)
                        tries++
                    }
                    if (tries > maxTries) {
                        Log.logger.warn("Cannot select page")
                        return false
                    }
                } catch (Exception ex) {
                    Thread.sleep(2000)
                    tries++
                }
            }
        } catch (Exception ex2) {
            Thread.sleep(2000)
            tries++
        }
    }
    return false;
}
chrismead
  • 2,163
  • 3
  • 24
  • 36