switchTo().frame() is not working when the iFrame is directly under the <body> tag

3,157 views
Skip to first unread message

Robins Mathew

unread,
Jul 4, 2016, 6:47:02 AM7/4/16
to Selenium Users

I have worked with iframes and I have never faced any issues with it so far. But recently I was having an issue with switching to an iframe. 

Please help if anybody have faced the same issue. I can resolve the issue by adding iframe to a div but thats not the right way to do it. I am wondering if this is a selenium bug

If the iframe is directly under the tag <body> <iframe></iframe> </body> switching is not working






























But if the same iframe is inside <body> <div>  <iframe></iframe> <div> </body> switching will work


Saravana Kumar N

unread,
Jul 20, 2016, 10:30:46 PM7/20/16
to Selenium Users
I am also facing the same issue.

Andrew Jervis

unread,
Jul 22, 2016, 12:23:53 PM7/22/16
to Selenium Users

Robins Mathew

unread,
Jul 25, 2016, 4:45:25 AM7/25/16
to Selenium Users
Hi Andrew,

Thank you for the reply. Unfortunately none of this works if the iFrame is a directly under the <body> tag

Thanks,
Robins

ms surya kumar

unread,
Jul 25, 2016, 6:42:47 AM7/25/16
to Selenium Users
Even I faced the same Problem when dealing with new application..

Mallik H

unread,
Jul 25, 2016, 9:24:14 AM7/25/16
to Selenium Users
Please give a try: below information might help (yet to test, sorry:)

Issue happens while trying to switch to frame using frame name or frame id ?
driver.switchto.frame(name/id)

And this issue is only on Windows platform.

Workaround is to switch to frame using webelement.
driver.switchto.frame(webElement

On Monday, July 4, 2016 at 4:17:02 PM UTC+5:30, Robins Mathew wrote:

Robins Mathew

unread,
Jul 25, 2016, 9:40:38 AM7/25/16
to Selenium Users
Hi Mallik,

I have tried all possible options.

driver.switchto.frame(name);
driver.switchto.frame(id);
driver.switchto.frame(WebElement);

Nothing works when the iFrame is in <body> tag

Robins Mathew

unread,
Jul 25, 2016, 9:42:01 AM7/25/16
to Selenium Users
Hi Surya,

Were your iframe firectly under <body>?

Thanks,
Robins

praveen kumar cherukuri

unread,
Jul 25, 2016, 10:00:39 AM7/25/16
to seleniu...@googlegroups.com
<<This is not answer to the issue nor any suggestion to try something..>> 

- Are you able to find the webelement in the dom (in a selenium browser)
 : You can print the page source before switching to the webelement and observe if the iframe is actually rendered or not. 

- I'm suspicious that the selenium focus is already on the iframe. 
 : Shouldn't be trivial thing. as you might have already tried this option by now. 

Just curious to know if there is a public version of your site.. or a sandbox version.. which can be shared. 



--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/aed2ed75-2f33-4a24-8bf5-dcae2f28fa61%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robins Mathew

unread,
Jul 25, 2016, 10:10:16 AM7/25/16
to Selenium Users
Hi Praveen,

Thanks for the inputs.

I am able to find the WebElement(iframe[class^='wc-UserPopups_Frame']) in the dom.

If I print the page source before switching to the webelement it displays iframe[class^='wc-UserPopups_Frame'] in the returned string.

Selenium focus is not on the iframe when i try to switch. It is in the parent window.

The same iFrame I have taken out from<body><\body> and placed it in a <div><\div> it worked. Here the problem is if iFrame is in the <body> tag it wont switch.
It happened to me in 2 applications. switching did not work when iFrame is directly under<body>

unfortunately both the application are not in public domain now.

Thanks,
Robins

Saravana Kumar N

unread,
Jul 25, 2016, 10:56:11 AM7/25/16
to Selenium Users
We tried almost all the options. Since we could not find a solution, we asked dev team to embed the IFrame in <div> tag, that's resolved our problem.

Thanks,
Saravana


On Monday, July 4, 2016 at 11:47:02 AM UTC+1, Robins Mathew wrote:

Mallik H

unread,
Jul 26, 2016, 9:23:57 AM7/26/16
to Selenium Users


On Monday, July 4, 2016 at 4:17:02 PM UTC+5:30, Robins Mathew wrote:

Mallik H

unread,
Jul 26, 2016, 9:31:14 AM7/26/16
to Selenium Users
Sorry my previous reply (which wasn't tested fully):

Below is the working code : (i) When the iframe is directly inside the body tag

for example:  <link rel="stylesheet" href="common.css">
  <link rel="stylesheet" href="signin.css">
</head>

<body>
<iframe src="http://www.w3schools.com"></iframe>
<div class="header" ng-controller="repLoginCtrl as repLoginCtrl">
  <header class="container-fluid">
    <div class="content"></div>
  </header>
  <div class="page-title">


public static void main(String[] arg) throws InterruptedException
    {
        WebDriver driver = new FirefoxDriver();
       
        driver.get("file:///D:/myhtml.html");
       
        List<WebElement> elements = driver.findElements(By.tagName("iframe"));
       
        WebElement element = null;
       
        for(WebElement ele: elements)
        {
            if(ele.getAttribute("src").contains("http://www.w3schools.com"))
            {
                element = ele;
                break;
            }   
        }
        driver.switchTo().frame(element);
        new WebDriverWait(driver, 60).until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//a[contains(@title, 'Translate W3Schools')]"))));
       
        System.err.println("href attribute value : "+ driver.findElement(By.xpath("//a[contains(@href, 'http://www.w3schools.com')]")).getAttribute("href"));       
        System.out.println("\n");
        driver.findElement(By.xpath("//a[contains(@title, 'Translate W3Schools')]")).click();

Robins Mathew

unread,
Jul 26, 2016, 9:35:12 AM7/26/16
to Selenium Users
Thanks for that Mallik,

I have not tested it with firefoxDriver. I am using ChromeDriver with RemoteWebDriver.

Since you already have the setup. Is it possible for you to do a quick run in ChromeDriver to see if it works?

Anyways I will try with FireFoxDriver

Mallik H

unread,
Jul 26, 2016, 9:53:06 AM7/26/16
to Selenium Users
Rebins Mathew,

I have attempted to run test script upon modification for chrome browser, but i'm facing chrome browser launch issue, as currently installed with older version of chrome browser on my desktop (error line : Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Chrome version must be >= 49.0.2623.0)

Request to give a try using below code for CHROME Browser:

        //WebDriver driver = new FirefoxDriver();
       
        System.setProperty("webdriver.chrome.driver", "C:" + File.separator + "chromedriver_win32" + File.separator + "chromedriver.exe");
       
        WebDriver driver = new ChromeDriver();

       
        driver.get("file:///D:/myhtml.html");
       
        List<WebElement> elements = driver.findElements(By.tagName("iframe"));
       
        WebElement element = null;
       
        for(WebElement ele: elements)
        {
            if(ele.getAttribute("src").contains("http://www.w3schools.com"))
            {
                element = ele;
                break;
            }   
        }
        driver.switchTo().frame(element);
        new WebDriverWait(driver, 60).until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//a[contains(@title, 'Translate W3Schools')]"))));
       
        System.err.println("href attribute value : "+ driver.findElement(By.xpath("//a[contains(@href, 'http://www.w3schools.com')]")).getAttribute("href"));       
        System.out.println("\n");
        driver.findElement(By.xpath("//a[contains(@title, 'Translate W3Schools')]")).click();

Robins Mathew

unread,
Jul 26, 2016, 10:50:38 AM7/26/16
to Selenium Users
Hi Mallik,

Thanks for the help. It worked with the sample website you have provides. But for some reason its not working with our application. It can be something different the way they construct an iFrame.

Anyways thanks for spending time on this. 

Thanks,
Robins

Biswajit Pattanaik

unread,
Aug 4, 2016, 8:06:54 AM8/4/16
to Selenium Users
Hi Robins,

Use this code it will work, In my project it's working fine.

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@name='XXXXXXXXXX']")));  //Switch to Frame

Regards
Biswajit
Reply all
Reply to author
Forward
0 new messages