Monday 26 February 2018

How To Handle Frames By Using Selenium WebDriver

 Switch to Frame By Using Selenium WebDriver

========================================================================

Switch To Frame:

1..A frame is document that is defined inside a parent document.
2. As the driver object can point to only one frame at a time, by default it points to the parent document.
3. So, inorder to perform any operation on the elements in the frame, first we need to switch to the respective frame.
4. Firepath view can handle only one document at a time.
5. In Order view the complete set of documents associated with current webpage, we need to move to HTML View.

1.Using this frame details, but the web element object.

WebElement Element =driver.findElement(By.Xpath(“//frame[@name=’’login_Page]”))

The following are the steps to switch to the frame


1.Create a webelement object to the respective frame.
2.Use this webelement object to switch to the frame.
3. Perform the desired operation in the frame.
4. Switch back to the parent document.

                                     Code to switch to frame:

WebElement frameobj = driver.findElement(By.xpath("//frame[@name='login_page']"));
driver.switchTo().frame(frameobj);


Code to switch back to parent document:

driver.switchTo().defaultContent();


Note:

If two frames are present adjacently, then
1. first we need to switch to one frame
2. switch to default content
3. switch to the second frame.


Example1:


WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.navigate().to("https://netbanking.hdfcbank.com/netbanking/");
WebElement ele = driver.findElement(By.xpath("//frame[@name='login_page']"));
driver.switchTo().frame(ele);
driver.findElement(By.xpath("//img[@alt='continue']")).click();
driver.switchTo().defaultContent();


Example 2:


WebDriver driver = new FirefoxDriver();
driver.get("https://netbanking.hdfcbank.com/netbanking/");
WebElement frameobj = driver.findElement(By.xpath("//frame[@name='login_page']"));
driver.switchTo().frame(frameobj);
driver.findElement(By.xpath("//input[@name='fldLoginUserId']")).sendKeys("40058155");
driver.switchTo().defaultContent();
WebElement frameobj2 = driver.findElement(By.xpath("//frame[@name='footer']"));
driver.switchTo().frame(frameobj2);
driver.findElement(By.xpath("//a[text()='Terms and Conditions']")).click();
         
Software Testing Courses To Contact Bellow Number

Testing Masters Technologies                                                    8790002007

No comments:

Post a Comment