Friday, May 27, 2011

Switching between frames and WebDriver

I have web page which has two frames -navigator and content.I have to click on a link in the navigator and the content of the frame 'content' changes.Now I have to input some value in the content frame.I use IDE  to record and generate code in the junit4 webdriver format.
        selenium.selectFrame("navigator");
        selenium.click("link=mylink");
        selenium.selectFrame("relative=up");
        selenium.selectFrame("content");
        selenium.type("myinputfield", "7");

Now when I try to run this code I get error at relative=up.So I google and find that this is not the way to write webdriver code.I rewrote the entire code in the webdriver style coding .So now I have this code.
        driver.switchTo().frame("navigator");
        WebElement my_nav_link = driver.findElement(By.partialLinkText("MyLinkText"));
        my_nav_link.click();
        driver.switchTo().frame("content");
        WebElement myinputfieldelem= driver.findElement(By.id("myinputfield"));
         myinputfieldelem.sendKeys("7");
Now when I try again , it errors out at frame not found for content .
So I try to debug a bit and I add the below code just before switch to frame to see all the frames present.
        List<WebElement> frameset=driver.findElements(By.tagName("frame"));
        if(frameset.size()>0)
        {
            for (WebElement framename :frameset)
            {
                System.out.println("frame id:" + framename.getAttribute("name"));
            }
        }
There are no frames ,so that means I am now in navigator frame and I have to go one level up to find the frame content.
So I tried 
         driver.switchTo().frame("relative=up");
This also failed.Now I am clueless and and could  not figure  out what to do.After some googling i found out another option which saved me  and the final code which works is as below.
         driver.switchTo().frame("navigator");
        WebElement my_nav_link = driver.findElement(By.partialLinkText("MyLinkText"));
        my_nav_link.click();
        driver.switchTo().defaultContent();
        driver.switchTo().frame("content");
        WebElement myinputfieldelem= driver.findElement(By.id("myinputfield"));
        myinputfieldelem.sendKeys("7");
 So the outcome is you have to use  switchTo().defaultContent() to be able to come out of a frame and go to a sibling frame.

27 comments:

  1. Hi Evelin,
    I am unable to execute switchTo to call another url inside of my widget farme. Here is my code driver.switchTo().frame("../pack/start?lang=en_US&owf=true"); but it is not working, it says, "No frame found." Do you have any other solution for me? please help.

    ReplyDelete
    Replies
    1. Hi Kobir,
      I am not sure If I got your question correctly.Here I am explaining about switching between frames within a webpage.The argument for switchTo().frame() should be the name of a frame within the webpage.
      If you want to open a new url, you can try out open(url) referred here -http://release.seleniumhq.org/selenium-core/0.8.0/reference.html.
      I have not used it myself.Try it and see.

      Delete
    2. Hi Evelin, If I have two web URL and if currently my control is in First Webpage then how to go the second webpage and search for the target frame. Any help will be appreciate. Thanks in advance. :)

      Regards,
      Mukesh

      Delete
  2. I was having issues with going from the Selenium IDE commands

    selectFrame relative=up
    selectFrame main

    to the webDriver equivalent.
    Thank you very much for posting this, the commands:

    driver.switchTo().defaultContent();
    driver.switchTo().frame("main");

    were exactly what I needed in order to continue through the script.

    ReplyDelete
    Replies
    1. the commands:
      driver.switchTo().defaultContent();
      driver.switchTo().frame("main");
      were exactly what I needed in order to continue through the script.


      Great !
      Thanks a lot !!
      It works also in case of "iframe" and with the id as parameter (when no name available).

      S.

      Delete
  3. thanks for the post.. this is really helpful..

    ReplyDelete
  4. Hi Evelin,
    I am working on iframe which has one html tag inside another html tag. After I switched to frame(FrameID) I can access all the object presented in the first html, but i cannot access the object presented in second html. Any Help??

    - Bala

    ReplyDelete
  5. Hi All,
    I am working on iframe which has one html tag inside another html tag. After I switched to frame(FrameID) I can access all the object presented in the first html, but i cannot access the object presented in second html. Any Help??

    - Bala

    ReplyDelete
  6. i have an issue that my frame that changing dynamically and do not have any name
    i m unable to switch in that frame any help buddies i tried alot but got nothing ............

    ReplyDelete
  7. hi,
    we can use driver.switchTo().frame(0); to switch in to that frame.If name and id are not present for frame.

    ReplyDelete
  8. |html>
    |frameset>
    |frameset>
    |frame>
    |html>
    |body>
    |input id='abc'>
    |/body>
    |/html>
    |/frame>
    |/frameset>
    |/frameset>
    |/html>


    How to find the Web Element "Input"?

    ReplyDelete
  9. This saved my day, thanks a lot.
    Does anyone here has contact to the Selenium guys, so they put this remark about the defaultContent() method on their website?

    ReplyDelete
  10. Hi Evelin,
    I am also working with multiple frames. In my situation, on clicking on a link in a frame, content of another frame changes. I need to verify if the other frame is loading or not. How to do that? I am able to switch between the frames.

    ReplyDelete
  11. Great post. It helped me.

    ReplyDelete
  12. WOW Thanks a lot!!!! it really helped me a loooooooot :)

    ReplyDelete
  13. Thank you very much, so helpful information!!

    ReplyDelete
  14. Thanks, it helped a lot

    ReplyDelete
  15. Solved my problem.. Thanks a tone..!!! :)

    ReplyDelete
  16. worked for me. Thanks.

    ReplyDelete
  17. Hi Evelin,
    I have been searching for this from past 1 day... Thanks a ton!!!

    ReplyDelete
  18. |html>
    |frameset> frameset have only "title" no "id" and no "name"
    |frame> frame have only "title" no "id" and no "name"
    |html>
    |body>
    |input class='abc'>
    |/body>
    |/html>
    |/frame>
    |/frameset>
    |/html>

    ReplyDelete
  19. |html>
    |frameset> frameset have only "title" no "id" and no "name"
    |frame> frame have only "title" no "id" and no "name"
    |html>
    |body>
    |input class='abc'>
    |/body>
    |/html>
    |/frame>
    |/frameset>
    |/html>


    please tell me how to access input

    ReplyDelete
  20. name = output.driver.find_element_by_class_name('yui-panel-container shadow yui-overlay-hidden')
    AttributeError: 'unicode' object has no attribute 'driver'

    I need any solution

    ReplyDelete