How to identify Frames

1,616 views
Skip to first unread message

divya angel

unread,
Nov 16, 2012, 2:18:00 PM11/16/12
to webd...@googlegroups.com
Hi can any one explain me in brief about the iframe concept in webdriver. i 've googled it and found the below code..and i was unable to understand how to identify the frames..
i will be thankful if  any one please take some little time to explain about this
------------------------
import static org.testng.AssertJUnit.assertEquals;

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 gmailTestNG {
private WebDriver driver;

@BeforeClass
public void Startup() {
driver = new FirefoxDriver();
}

@Test(description = "Google Login")
public void GoogleLogin() throws Exception {

// driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.gmail.com");
assertEquals("Sign in", driver.findElement(By.id("signIn"))
.getAttribute("value"));
driver.findElement(By.id("Email")).sendKeys("iamaauto");
driver.findElement(By.id("Passwd")).sendKeys("chebrolu");
driver.findElement(By.id("signIn")).click();
Thread.sleep(10000);
driver.switchTo().frame("canvas_frame");
driver.findElement(By.id("gbgs4dn")).click();
driver.findElement(By.id("gb_71")).click();
driver.switchTo().defaultContent();
assertEquals("Sign in to Gmail", driver.findElement(By.id("button"))
.getText());
}

@AfterClass
public void teardown() {
driver.quit();
}
}




waiting for positive response...

Apha

unread,
Nov 16, 2012, 3:10:58 PM11/16/12
to webd...@googlegroups.com
Code its pretty straight forward when you wann ur webdriver to start executing inside some iframe u have to use switchTo() method which inturn tells webdriver to change target ie moving from main page to iFrame ... because iframe can have its own body and other elements ... when you write driver.switchTO.frame("canvas_frame") you r telling webdriver to get control of iframe having id/name as canvas_frame. and when you write driver.switchTo().defaultContent() it moves back to main frame 

hope that helps 

A Pha

divya angel

unread,
Nov 16, 2012, 4:44:06 PM11/16/12
to webd...@googlegroups.com
thanx  A Pha for ur reply,

i am sorry for asking this again..bcause webdriver was new to me and i never used identifying elements using frames..so kindly explain the topic.. 

my trouble was how to identify the specific frame in the source code....

      suppose if i want to click on the compose button in the gmail page...then what will be the frame id and to get that id,name,class r any thing? 
how do we identify the frame?

hope u got my problem...

Apha

unread,
Nov 16, 2012, 5:15:00 PM11/16/12
to webd...@googlegroups.com
do you know how to inspect element using Firefox's firepath/xpath checker  and firebug ? or chrome's developers tools ? well I never worked with gmail page but if u wann hit compose button then first see the HTML elements then u will find compose is a div its not inside any iframe so you need to write xpath something like //div[@class='T-I J-J5-Ji T-I-KE L3'].

If i understand it correct your question is how to get handle to iframe and elements inside ... best practice is every element to have unique id if not u can always use xpath to get to the frame ( firepath, xpath checker will help you) say you have code something like this 

<div id="xyz">
 <iframe id="newIframe"> <input type=text id="textBox" /> </iframe>
</div>

now xpath of above iframe will be something like "//div[@id='xyz']/iframe" this will get an iframe here use can use webdriver.switchto().frame(webdriver.findelement(By.xpath("//div[@id='xyz']/iframe"))); now web drivers target is iframe to get any elements now onwards on iframe u just have to do webdriver.getElememnt(By.id("textBox")); this is how u play with iframes 

hope I was able to clear something 


SANTHOSH BABY

unread,
Nov 16, 2012, 11:52:20 PM11/16/12
to webd...@googlegroups.com
When you spy an element using firbug or chrome developer over a element . you could easily notice whether it contains iframe etc ..

Pretty basic level ..

Spend time using firebug identifying object you would be able to justify your question yourself

Thank you
Cheers.
rsz_santhosh.jpg
Santhosh Baby

Santhosh Software Automation  Engineer
 



Think GREEN. Please consider the environment before printing this email

"When The Winds of Change Blow..... Some people Build Walls & Others Windmills.... Attitude Matters...."





--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/mUYnqkC140oJ.

To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

rsz_santhosh.jpg
image001.gif

Dibyaranjan Kar

unread,
Nov 17, 2012, 11:19:12 AM11/17/12
to webd...@googlegroups.com

darrell

unread,
Nov 17, 2012, 11:53:40 AM11/17/12
to webd...@googlegroups.com
First, have you had a look at http://darrellgrainger.blogspot.ca/2012/04/frames-and-webdriver.html. The trick is to break the page into multiple 'pages' by treating each iframe as a separate page.

Second, gmail is using all the tricks in the book. If you are trying to learn ANYTHING from gmail it is complicated by the fact that it is mixed with everything else. So learning iframes while dealing with ajax, javascript, asynchronism, frames, etc. at the same time. Bottom line, I don't even try working with gmail. It is better to find a site which has tutorials for developers and use that. A tutorial on frames should have examples of JUST frames. I'd use that website to practice WebDriver using frames.

Darrell 

Sasi kumar

unread,
Nov 19, 2012, 12:51:35 AM11/19/12
to webd...@googlegroups.com
First you have to identify the frames as suggested by all. you can use driver.findElements(By.tagName("frame")) ,to get all frames.
Reply all
Reply to author
Forward
0 new messages