1

I have a problem to switch properly to frame.

The project is maven project with this pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>betlista</groupId>
    <artifactId>bugs.chromedriver</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <dependencies>

        <dependency>
            <groupId>com.github.detro</groupId>
            <artifactId>phantomjsdriver</artifactId>
            <version>1.2.0</version>
        </dependency>

    </dependencies>

</project>

My testing class is:

package bugs.chromedriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class Test {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "c:/betlista/progs/chromedriver_2.13_win32/chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        driver.get("http://localhost:8080/frames/chrome_test.html");
        driver.switchTo().frame(0); // outer
        try {
            driver.switchTo().frame(1); // menu - ERROR
        } catch (Exception e) {
            e.printStackTrace();
        }
        driver.quit();
    }

}

page I used for testing is

<html>
    <head>
        <title>Chome frames test</title>
    </head>

    <frameset rows="100%, *">
        <frame src="./outer.html">
    </frameset>
</html>   

outer frame:

<html>
    <head>
        <title>Chome frames test - outer</title>
    </head>

    <frameset rows="55, *">
        <frame src="top.html">
        <frameset cols="190,*">
            <frame src="menu.html" name="menu">
            <frame src="content.html"name="content">
        </frameset>
    </frameset>
</html>   

top frame:

<html>
    <head>
        <title>Chome frames test - top frame</title>
    </head>

    <body>
        top
    </body>
</html>   

menu frame:

<html>
    <head>
        <title>Chome frames test - menu frame</title>
    </head>

    <body>
        <div>menu</div>
    </body>
</html>   

content frame:

<html>
    <head>
        <title>Chome frames test - content frame</title>
    </head>

    <body>
        content
    </body>
</html>   

When I run the code exception occurs:

Starting ChromeDriver 2.13.307647 (5a7d0541ebc58e69994a6fb2ed930f45261f3c29) on port 25190
Only local connections are allowed.
org.openqa.selenium.NoSuchFrameException: no such frame
  (Session info: chrome=39.0.2171.95)
  (Driver info: chromedriver=2.13.307647 (5a7d0541ebc58e69994a6fb2ed930f45261f3c29),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 20 milliseconds
Build info: version: '2.41.0', revision: '3192d8a6c4449dc285928ba024779344f5423c58', time: '2014-03-27 11:29:39'
System info: host: 'CZCHOWN5019295', ip: '2.252.210.20', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_67'
Session ID: b30602d7260eefcf7e7c30b348271eb0
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=C:\Users\msuska\AppData\Local\Temp\scoped_dir29108_23597}, rotatable=false, locationContextEnabled=true, mobileEmulationEnabled=false, version=39.0.2171.95, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=false, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
    at org.openqa.selenium.remote.RemoteWebDriver$RemoteTargetLocator.frame(RemoteWebDriver.java:857)
    at bugs.chromedriver.Test.main(Test.java:15)

phantomjsdriver is using selenium chrome driver in version 2.41.0 so I tried also to upgrade to last version 2.44.0, one have to solve few problems:

When dependency is added, there is java.lang.ClassNotFoundException: com.google.gson.JsonElement so I had to manually add gson dependency - new pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>betlista</groupId>
    <artifactId>bugs.chromedriver</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <dependencies>

        <dependency>
            <groupId>com.github.detro</groupId>
            <artifactId>phantomjsdriver</artifactId>
            <version>1.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>2.44.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3.1</version>
        </dependency>

    </dependencies>

</project>

next there is org.openqa.selenium.WebDriverException: unknown error: cannot parse capability: chromeOptions, Test source needs to be modified slightly:

// ChromeDriver driver = new ChromeDriver(); // old
DesiredCapabilities cdc = DesiredCapabilities.chrome();
ChromeDriver driver = new ChromeDriver(cdc);

but same error again - org.openqa.selenium.NoSuchFrameException: no such frame.

The same works fine, when ChromeDriver is replaced with PhantomJSDriver.

There is old Chrome driver issue.

edit:
I believe, that this is error - the best understanding I got from window.frameElement description.

It seems to me, like switchTo should work like this:

  1. at the beginning there is one frame - from JS window.frames.length returns 1
  2. so I have to "switch" to first frame and window.frames[0].frames.length returns 3
  3. the second on is menu
Betlista
  • 10,327
  • 13
  • 69
  • 110
  • Possible duplicate of [How to switch between frames in Selenium WebDriver using Java](https://stackoverflow.com/questions/10879206/how-to-switch-between-frames-in-selenium-webdriver-using-java) – kenorb Aug 23 '17 at 16:20

3 Answers3

2

Try adding: driver.switchTo().defaultContent(); before switching to a new frame.

kenorb
  • 155,785
  • 88
  • 678
  • 743
1

Your problem is here:

driver.switchTo().frame(0); // outer
...
driver.switchTo().frame(1); // menu - ERROR

The first switchTo() will switch to your frame, not the outer frame as your comment incorrectly points out. The second switchTo() will fail, since there is no frame that is contained in there.

The solution is to drop the first switchTo(), since even your intent would effectively do nothing, and make the second driver.switchTo().frame(0); // menu.

SiKing
  • 10,003
  • 10
  • 39
  • 90
0

It seems to me, there is a problem with index based navigation only.

Trying to achieve the same by tag names works as expected (in both 2.41.0 and 2.44.0 dependency versions)

    List<WebElement> frames = driver.findElementsByTagName("frame");
    driver.switchTo().frame(frames.get(0));
    List<WebElement> frames2 = driver.findElementsByTagName("frame");
    driver.switchTo().frame(frames2.get(1));
Betlista
  • 10,327
  • 13
  • 69
  • 110