December 31, 2012

Switch between Frames using webdriver

Here is the simple example how to switch between frames.


There are three ways to switch between frames.

INDEX:
Frame(int index)
Select a frame by its (zero-based) index. That is, if a page has three frames, the first frame would be at index "0", the second at index "1" and the third at index "2". Once the frame has been selected, all subsequent calls on the WebDriver interface are made to that frame.

ID OR NAME:
 frame(java.lang.String nameOrId)
Select a frame by its name or ID. Frames located by matching name attributes are always given precedence over those matched by ID.
Frame(WebElement frameElement)
Select a frame using its previously located webelement
Parameters: frameElement - The frame element to switch to.
DEFAULTCONTENT()
Selects either the first frame on the page, or the main document when a page contains iframes.
Below example explain you how to switch between frames.
=============================================================
package testng; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class Frames { public WebDriver driver; @BeforeClass public void beforeClass() { driver=new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); } @Test public void testFrames() throws Exception { driver.navigate().to("http://jqueryui.com/"); driver.findElement(By.linkText("Autocomplete")).click(); //index // driver.switchTo().frame(0); //id or Name //in this example Frame is not having ID or Name so this is invalid here //driver.switchTo().frame("nameorid"); //frameElement driver.switchTo().frame(driver.findElement(By.className("demo-frame"))); //type in text box.. driver.findElement(By.id("tags")).sendKeys("java"); Thread.sleep(5000); //switch back to default content driver.switchTo().defaultContent(); driver.findElement(By.linkText("Accordion")).click(); Thread.sleep(5000); } @AfterClass public void afterClass() { driver.quit(); } }

18 comments:

  1. I blog quite often and I really appreciate your content.
    Your article has really peaked my interest. I am going to bookmark your blog and keep checking for new details about once a week.
    I opted in for your Feed as well.
    Feel free to surf my web blog ... skin bleach

    ReplyDelete
  2. What's up Dear, are you actually visiting this site regularly, if so after that you will definitely get good know-how.
    My web site : how to lose man breast

    ReplyDelete
  3. May I just say what a relief to find someone who truly understands
    what they're discussing on the internet. You actually understand how to bring a problem to light and make it important. A lot more people must read this and understand this side of the story. It's surprising you're not more popular given that you definitely possess the gift.
    Feel free to surf my blog :: http://nigeriain.com

    ReplyDelete
  4. May I just say what a relief to find someone
    who truly understands what they're discussing on the internet. You actually understand how to bring a problem to light and make it important. A lot more people must read this and understand this side of the story. It's surprising you're not more popular given that you definitely possess the gift.
    my web site :: http://nigeriain.com

    ReplyDelete
  5. Remarkable! Its really awesome article, I have
    got much clear idea on the topic of from this paragraph.



    Here is my blog post: forex trading made easy

    ReplyDelete
  6. Hey very interesting blog!

    My website - best online Jobs

    ReplyDelete
  7. Write more, thats all I have to say. Literally, it
    seems as though you relied on the video to make your point.
    You obviously know what youre talking about, why throw away your
    intelligence on just posting videos to your blog when you could
    be giving us something enlightening to read?

    Also visit my webpage ... ways to make extra money

    ReplyDelete
  8. always i used to read smaller articles that also clear their motive, and that is also happening
    with this article which I am reading now.

    Visit my web blog - forex classes

    ReplyDelete
  9. Having read this I thought it was extremely enlightening.
    I appreciate you taking the time and effort to put this information together.
    I once again find myself personally spending a lot of time
    both reading and posting comments. But so what, it was still worth it!


    Feel free to surf to my page; make money online now

    ReplyDelete
  10. If you are going for best contents like me, simply go
    to see this web page all the time because it presents quality contents, thanks

    my web-site ... earning money online

    ReplyDelete
  11. This is my first time visit at here and
    i am in fact impressed to read all at one place.


    Also visit my blog post: how to make fast money online

    ReplyDelete
  12. Just want to say your article is as astounding. The clearness in your post is just great and i could assume you
    are an expert on this subject. Fine with your permission allow me to
    grab your RSS feed to keep up to date with forthcoming post.
    Thanks a million and please keep up the rewarding work.


    Check out my blog ... how to make a lot of money fast

    ReplyDelete
  13. Hello! I simply would like to offer you a huge thumbs up for your excellent
    information you have right here on this post. I am coming back to
    your site for more soon.

    Also visit my web site work online from home

    ReplyDelete
  14. I visited multiple web sites except the audio feature for audio songs current at this web
    page is actually marvelous.

    Have a look at my web page how can i Make extra money

    ReplyDelete
  15. When someone writes an post he/she keeps the idea of a user
    in his/her brain that how a user can know it.
    Thus that's why this paragraph is amazing. Thanks!

    My blog post; make money free online

    ReplyDelete
  16. You need to take part in a contest for one of the greatest
    sites online. I'm going to highly recommend this web site!

    My blog :: I need money today for free
    My website - i need money now for free

    ReplyDelete
  17. Fabulous, what a weblog it is! This weblog presents helpful data
    to us, keep it up.

    my webpage - how to make money fast without a job

    ReplyDelete
  18. Amazing & Great informative blog,it gives very useful practical information to developer like me. Besides that Wisen has established as Best Java Online Training from India . or learn thru Online Training mode Hibernate Online Training . Nowadays Hibernate ORM has tons of job opportunities on various vertical industry. or learn thru JavaScript Online Training from India. Nowadays even JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete