6

I need to get the result from the table "td". But before I can do that I need to navigate a frame that contains it. The frame is one of the frameset elements that belongs to the mainFrame. I tried to use all types of navigating the "child" subframe non of which works:

 driver.switchTo().defaultContent();
    Thread.sleep(1000);
     driver.switchTo().frame("mainFrame.0.fr_resultsNav~ResultsMaxGroupTemplate0.9766101221774707");
    driver.switchTo().frame("main.Frame.1.fr_resultsNav~ResultsMaxGroupTemplate0.8811790466176727");

// even: driver.switchTo().frame("mainFrame.0.fs_main");

The following is the brief layout of the webpage:

<frame src="banner.asp" name="topFrame" scrolling="no" noresize="noresize" id="topFrame" title="topFrame">

<frame src="" name="mainFrame" id="mainFrame" title="mainFrame" wd_frame_id_="5f4c10bc7e0960070bfda831655b8b0c">

    <frameset id="fs_main" border="0" frameborder="no" framespacing="0" rows="70,87,*">
.....................

        <frameset id="fs_content" cols="23%,*" framespacing="0" frameborder="no">
.....................
            <frameset cols="*,9" id="LeftFrameSet" framespacing="0" frameborder="no">
.....................
            <frame frameborder="0" name="fr_classification~ResultsMaxGroupTemplate0.609021034867735" title="Results Classification Frame" id="fr_classification~ResultsMaxGroupTemplate0.609021034867735" src="/lnacui2api/results/shared/waitMessage.do?wmibIdRand=61_T16938265013_rand_1363544453847" scrolling="Auto" onload="paintResultsBorder('ResultsMaxGroupTemplate0.609021034867735');">

....................

<form name="results_listview_ResultsListForm" method="post" action="/lnacui2api/results/listview/listview.do" id="results_listview_ResultsListForm">

..........

<td nowrap="" height="20"> <span id="new">&nbsp;All Results</span> (294)</td>
</form>

....................

Do I need to navigate the frameset before I can navigate a subframe? I read the documentation. All internet examples give a simple sample code: driver.switchTo().frame("mainFrame.0.child"). It does not work in this case. Please, take a look at the above script.

5 Answers 5

10

I agree, you cannot directly switch to a child frame. Also, make sure to switch to the defaultContent (driver.switchTo.defaultContent) every time you want to switch frame. With regard to your example, driver.switchTo().frame("mainFrame.0.child") --- this could also work, but you need to get rid of unnecessary quotation marks.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Switching back to the default content before switching to a new frame is key. For anyone working in python, the corresponding method is: driver.switch_to_default_content()
@CHEBURASHKA, link is not working. Can you please update?
8

Find the index of main frame starting from zero then use

driver.switchTo.frame(mainFrameindex);

Then find the index of sub frame in the main frame

driver.switchTo.frame(subFrameIndex);

You cannot directly switch to a child frame without first switching to the parent frame. This is how it works.

1 Comment

will this work with IFrames? I have a list of both mixed: List<WebElement> frames = driver.findElements(By.xpath("//iframe")); frames.addAll(driver.findElements(By.xpath("//frame")));
3

Through chaining methods together, once you switchTo().defaultContent, you can create temporary lists of available frames through findElements() by tagName and go to that specific frames' index...

For example

driver.switchTo()defaultContent();
driver.switchTo().frame(driver.findElement(By.tagName("frameset")).findElements(By.tagName("frame")).get(2));

1 Comment

worked for me.! In my case I need to locate to nested frame: driver.switchTo().frame(driver.findElement(By.tagName("frameset")).findElements(By.tagName("frame")).get(1)); // switch to the second frame in main page driver.switchTo().frame(driver.findElement(By.tagName("frameset")).findElements(By.tagName("frame")).get(0)); //switch to the nested frame
1

You can switch directly to the frame you want with the xPath. Get the Xpath through the Developer console, then:

driver.switch_to.frame(driver.find_element_by_xpath('html / frameset / frameset / frame[1]'))

2 Comments

Doesn't seem to work for me. The course I'm taking tells you cannot do that. Others on similar questions say you cannot do that. My trials seem to go this way.
I also find that this does NOT work.... my target is of the form: html/frameset/frameset[1]/frame[2].
0

As multiple answers already suggested that we cannot directly switch to a child frame.
There is an easy way, using SelectorsHub we can get the xpaths of all the iframes in the path till the target element.

  • SelectorsHub can be added as chrome extension and it will be visible inside the inspect.

  • Make sure you restart browser after installing this extension.

  • Sometimes you might not see SelectorsHub tab in inspect window as shown because its there in the expand window (>> symbol).

    enter image description here

  • Above image shows, selectorshub gives the xpath of all iframes we need to switch to and the xpath of the target element we selected.

  • This really helps when there are multiple nested iframes.

<iframe frameborder="0" id="iframeResult" name="iframeResult" allowfullscreen="true" style="" xpath="1">
    <html xpath="1"><head></head><body contenteditable="false" style="">
        <h2>HTML Iframes</h2>
        <p>You can use the height and width attributes to specify the size of the iframe:</p>
        
            <iframe src="demo_iframe.htm" height="200" width="300" title="Iframe Example">
                <html xpath="1"><head></head><body style="">
                <h1 style="">This page is displayed in an iframe</h1>
            </iframe>
        
        </body></style>
    </html>
</iframe>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.