*** VImp Frame handling:
Frame divides the whole page into smaller pages.
main page - I want to have 2 or 3 small pages
we can insert different html pages in one page.
HTML TAGS:
----------
<html>
<iframe src="C:\brahma\Practise\qtp practise\web
apps\May222023\SampleWebPage.html" >
</iframe>
<iframe src=".\employeePage.html" > </iframe>
</html>
--------------------
Methods to Switch Frames in Selenium WebDriver:
1. Switch to Frame by Name or ID:
- Use the
frame(String nameOrId)method to switch to a frame using itsnameoridattribute. - driver.switchTo().frame("frameNameOrId");
2. Switch to Frame by Index:
- Use the frame(int index) method to switch to a frame
using its index (zero-based).
java
Copy code
driver.switchTo().frame(0); // Switches to the
first frame
3. Switch to Frame by WebElement:
- Locate the frame element
first using findElement() and then switch to it using
the frame(WebElement
frameElement)
method.
java
Copy code
WebElement frameElement =
driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(frameElement);
------------------------------
Selenium code:
// 1.switch to frame by name
package AlertFrameWindowsBasics;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class HandlingFrame1 {
public static
void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
".\\Drivers\\chromedriver.exe");
// .
Represents current project folder name
// //open chrome browser
WebDriver driver =
new ChromeDriver();
//
get("url ") - to open the url in chrome browser
driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/iframes.html");
//***********************************
//
Enter some value in "Test' textbox
driver.findElement(By.name("myname")).sendKeys("Testing");
//Enter
some vlaue in first name text box
// driver.findElement(By.name("firstname")).sendKeys("My
first name");
// Exception in thread
"main" org.openqa.selenium.NoSuchElementException: no such element:
Unable to locate element: {"method":"css
selector","selector":"*[name='firstname']"}
//
even if the first name ele is present on the page, it throws "NoSuchElementException"
// 1st We have to check the "first
name" text box ele is in "Frame" or not
// Go
to frame -1 and enter 'Swathi' in first name textbox
// we have to switch ctrl
to frame by "name" or
"id"
// driver.switchTo().frame("iframe1name");
//pass
invalid frame name-("iframe1name123");
driver.switchTo().frame("iframe1name123");
// Exception
in thread "main" org.openqa.selenium.NoSuchFrameException: No frame
element found by name or id iframe1name123
//
swicth to frame by invalid name
// org.openqa.selenium.NoSuchFrameException: No
frame element found by name or id iframe1nameNEW
// if we switch to frame with invalid frame name = 'iframe1nameNEW'
which is not availabe in page,
//it
throws NoSuchFrameException
//
Enter 'Swathi' in first name text box
driver.findElement(By.name("firstname")).sendKeys("Swathi");
//
Enter 'Ranganathan' in last name text
box
driver.findElement(By.name("lastname")).sendKeys("Ranganathan");
//
come back to main page
driver.switchTo().defaultContent();
// HW
:Go To Frame -2 and enter 'sita' in username, enter 'mercury ' in pwd and Go back to main page
//HW Go to Frame -3 and click button 2 times
//HW
Enter 'Usha'in 'Test' text box
}
}
HW 1.2 Switch to
Frame by "Id" :
package AlertFrameMultipleBrowserWindowsBasics;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class HanldingFrameBySwitchingToFrameUsingId {
public static
void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
".\\Drivers\\chromedriver.exe");
// .
Represents current project folder name
// //open chrome browser
ChromeDriver driver =
new ChromeDriver();
//
get("url ") - to open the url in chrome browser
driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/iframes.html");
//
Enter 'Raju' in Test txt box
// Go
to frame -1 and and enter 'Swathi' in first name textbox
// we have to switch ctrl
to frame by id
//
Enter 'Swathi' in first name text box
//
Enter 'Ranganathan' in last name text
box
//switch
control to main page
//
swicth to frame by invalid name -("iframe1nameNEW");
//
org.openqa.selenium.NoSuchFrameException: No frame element found by name
or id iframe1nameNEW
// if we switch to frame with given name = 'iframe1nameNEW' which is
not availabe in page,
//it
throws .NoSuchFrameException
// HW
:Go To Frame -2 by 'id' and enter 'sita' in username, enter 'mercury ' in
pwd and Go back to main page
//
remove the data in 'Test textbox'
//HW Go to Frame -3 by id
and click button 2 times
//HW
Enter 'Usha'in 'Test' text box
}
}
2. Switchto frame
by "index":
Frame index no starts with 0,1,2...
Frame-1 has indexno =0
Frame-2 has indexno =1
Frame-3 has indexno =2
code:
package AlertFrameWindowsBasics;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class HandlingFrameBySwitchingToFrameUsingIndexNo {
public static
void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
".\\Drivers\\chromedriver.exe");
// .
Represents current project folder name
// //open chrome browser
WebDriver driver =
new ChromeDriver();
//
get("url ") - to open the url in chrome browser
driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/iframes.html");
///**********************************
//
Enter 'Raju' in Test txt box
driver.findElement(By.name("myname")).sendKeys("Raju");
// Go
to frame -1 and and enter 'Swathi' in first name textbox
//
1st we have to switch ctrl to frame
by "index no "
// driver.switchTo().frame(0);
//
switch to frame by invalid indexno = 7
driver.switchTo().frame(7);
// Exception in thread "main"
org.openqa.selenium.NoSuchFrameException: no such frame
// Command: [84045d2c7ccba2990179544e8d9d3463,
switchToFrame {id=7}]
// if we switch to frame with invalid index no =7, which is not
availabe in page,
//it
throws .NoSuchFrameException
//
Enter 'Swathi' in first name text box
driver.findElement(By.name("firstname")).sendKeys("Swathi");
//
Enter 'Ranganathan' in last name text
box
driver.findElement(By.name("lastname")).sendKeys("Ranganathan");
//switch
control to main page
driver.switchTo().defaultContent();
//HW Go To Frame -2 by indexno and enter 'sita' in
username by switching to frame by using index
//
switch to frame -2 by using indexno
//Hw
enter 'mercury' in password
//HW
switch to main page
//HW
Go to Frame -3 by 'index no' and click
button 2 times by switching to frame by
using index
//HW
Go to Frame -2 using 'index no' , Enter
'ur name' in un text box and pwd = "ur name", Click 'Reset New
'button
}
}
3. Switch to
Frame by "web Element" :
switchto(ele);
ele- find the iframe ele
Selenium code:
package AlertFrameWindowsBasics;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class HandlingFrameBySwitchingToFrameUsingWebElement {
public static
void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
".\\Drivers\\chromedriver.exe");
// .
Represents current project folder name
// //open chrome browser
WebDriver driver =
new ChromeDriver();
//
get("url ") - to open the url in chrome browser
driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/iframes.html");
//
Enter 'mercury" in 'Test' textbox elemet
driver.findElement(By.name("myname")).sendKeys("Raju");
// switch ctrl to frame 1 by using
"webelement"
//
identify frame element
-//iframe[@name='iframe1name']
WebElement frame1Ele=
driver.findElement(By.xpath("//iframe[@name='iframe1name']"));
driver.switchTo().frame(frame1Ele);
//
Enter 'Swathi' in first name text box
driver.findElement(By.name("firstname")).sendKeys("Swathi");
//
Enter 'Ranganathan' in last name text
box
driver.findElement(By.name("lastname")).sendKeys("Ranganathan");
// must switch ctrl to main page
driver.switchTo().defaultContent();
//HW Go To Frame -2 and enter 'sita' in
"username" by switching to frame by using webelement
//Hw
Go to Frame -3 and click button 2 times by switching to frame by using
webelement
//HW
click 'Click me!' 2 times
Thread.sleep(2000);
//
HW switch to default main page
}
}
-----------------------------------------
*** VImp FAQ how
can you handle frames?
3 ways
1.Switchto().frame(name or id)
2.Switchto().frame(indexno)
3.Switchto().frame(iframeEle)
no ele -->
no alert -->
No Frame -->
no frame -- if no frame is
there , but we are trying to switch to
that frame - we get ' NoSuchFrameException'
driver.switchTo().frame(5);
// as we dont have frame -5 in web page, it throws below
//
" org.openqa.selenium.NoSuchFrameException: no such frame
Note:
Even if xpath or locator is
right, some times it throws NosuchElementException.
When we get this exception, Check that element is in frame or not.
if ele is in frame,first we have to switch control to frame ..And
perform actions on the elements.
Once u perform actions on frame elements, we must switch control
to main page.
// HW get all frames count in page ?
package AlertFrameMultipleBrowserWindowsBasics;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class FramesCnt {
public static
void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
".\\Drivers\\chromedriver.exe");
// .
Represents current project folder name
// //open chrome browser
ChromeDriver driver =
new ChromeDriver();
//
get("url ") - to open the url in chrome browser
driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/iframes.html");
//
get frames count -- Iframe --
findElement() findElements()
//
import package - ctrl + shft + o
System.out.println("framesCnt=");
//
framesCnt=4
// HW
get Frames count By.tagname()?
}
}
How to check
Frames in page :
Right click on frame >
We can view options i.e View Frame source,Reload Frame
check frames in html page :
-------------------------
write xpath - //iframe
-------------------------------------
// HW WAP to switch to all frames and get all textboxes count ?
o/p:
Frame-1 , textboxes count = 4
Frame-2 , textboxes count = 4
Frame-3 , textboxes count = 4
Challenges in
PEGA application/ Project :
Frame name and id properties changing frequently?
<iframe name= "PegaFrame101"
<iframe name= "PegaFrame103"
//iframe[contains(@name,'PegaFrame')]
//iframe[starts-with(@name,'PegaFrame')]
HW find 'un' textbox field's Frame no by switching to each frame
getFrameNoOFGivenElement(String xpath );
//frame no = 1
ex2:
getFrameNoOFGivenElement("//input[@name='firstname']");
// Frame no 0
HW find 'Click me ' button's Frame no by switching to each frame?
HW find frame no of 'un'
text box,switch to that frame no and enter 'raju' in un textbox ?
Inner Frame /
Nested Frame:
Frame inside the frame is called Nested frame.
package AlertFrameWindowsBasics;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class NestedFrame {
public static
void main(String[] args) throws InterruptedException {
//
TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver",
".\\Drivers\\chromedriver.exe");
// .
Represents current project folder name
// //open chrome browser
WebDriver driver =
new ChromeDriver();
//
get("url ") - to open the url in chrome browser
driver.get("https://chercher.tech/practice/frames-example-selenium-webdriver");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
//Enter
"My topic " in Topic text box
// driver.findElement(By.id("topic")).sendKeys("My
topic");
//
NoSuchElementException if ele is present on the page, 1st check the ele is in frame or not
// Thread.sleep(6000);
//
switch to frame-1
// driver.switchTo().frame("frame1");
WebElement
iframe1Ele=
driver.findElement(By.xpath("//iframe[@id='frame1']"));
driver.switchTo().frame(iframe1Ele);
//Enter
"My topic " in Topic text box
// driver.findElement(By.id("topic")).sendKeys("My
topic");// not working
//
ElementNotInterctableException
// on the label name, we cannot send data
// driver.findElement(By.xpath("//input[@type='text']")).sendKeys("inner
frame");
driver.findElement(By.xpath("//b[@id='topic']/following-sibling::input")).sendKeys("Inner
frame");
/// Error:NoSuchEle this ele is in frame -1
//
click 'Inner Frame Check box :'
//
Switch to inner frame
driver.switchTo().frame("frame3");
//
click 'Inner Frame Check box :'
driver.findElement(By.id("a")).click();
//
switch control back to main page
driver.switchTo().defaultContent();
Thread.sleep(6000);
//
Switch to frame-3 and Select 'Avatar'
in "Animals" dropdown
driver.switchTo().frame("frame2");
WebElement animalsDropdownEle =
driver.findElement(By.id("animals"));
Select
sel = new Select(animalsDropdownEle);
sel.selectByVisibleText("Avatar");
//
switch control back to main page
driver.switchTo().defaultContent();
}
}
FAQ How can you handle inner Frame/Nested Frame ?
switch to frame-1
Switch to inner
frame.
No comments:
Post a Comment