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.

26 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.

    Reply
    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.

    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

    3. Hi,
      Please send some docs..

  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.

    Reply
    Replies
    1. Anonymous

      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.

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

    Reply
  4. Merry Christmas Evelin

    Reply
  5. Excellent piece of information..

    Reply
  6. 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

    Reply
  7. 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

    Reply
  8. Anonymous

    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 ............

    Reply
  9. Anonymous

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

    Reply
  10. |html>
    |frameset>
    |frameset>
    |frame>
    |html>
    |body>
    |input id='abc'>
    |/body>
    |/html>
    |/frame>
    |/frameset>
    |/frameset>
    |/html>


    How to find the Web Element "Input"?

    Reply
  11. 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?

    Reply
  12. 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.

    Reply
  13. Karthik

    Great post. It helped me.

    Reply
  14. Thanks.it helped me :)

    Reply
  15. WOW Thanks a lot!!!! it really helped me a loooooooot :)

    Reply
  16. Anonymous

    Thank you very much, so helpful information!!

    Reply
  17. Anonymous

    Thanks, it helped a lot

    Reply
  18. Anonymous

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

    Reply
  19. Anonymous

    worked for me. Thanks.

    Reply
  20. Anonymous

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

    Reply
  21. |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>

    Reply
  22. |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

    Reply