Automation QA Testing Course Content

How to Handle Frames in Selenium WebDriver?

Frames are two types
1)normal frames:normal frames are represented in html by
<frame > tag

//how can you switch to normal frames
driver.switchTo().frame("framelocator");
How can you switch based on the position of the frame
driver.switchTo().frame(int index);

2)internal frames:these are represented in html by <iframe>
tag

how to handle internal frames:

1)identify the internal frame
WebElement ifrmname=driver.findElement(By.locator("locatorvalue"));

//switching to the internal frame
driver.switchTo().frame(WebElement frameelementname);
driver.switchTo().frame(ifrmname);

//how can you switch back from frame/iframe to original
position
driver.switchTo().defaultContent();
(or)
driver.navigate().back();

How do you know howmany frames/iframes are in a webpage?
List<WebElement>frms=driver.findElements(By.tagName("frame/iframe"));

//print the number of frames
SOP(frms.size());

Frame Exception:
Exception in thread "main" org.openqa.selenium.NoSuchFrameException: No frame element found by name or id classFrame

==============================================
public void switchToFrame(int frame) { try { driver.switchTo().frame(frame); System.out.println("Navigated to frame with id " + frame); } catch (NoSuchFrameException e) { System.out.println("Unable to locate frame with id " + frame + e.getStackTrace()); } catch (Exception e) { System.out.println("Unable to navigate to frame with id " + frame + e.getStackTrace()); } }
==========================================================================================

driver.switchTo().frame(String arg0);

Select a frame by its name or ID. Frames located by matching name attributes are always given precedence over those matched by ID.
Parameters: name Or Id - the name of the frame or the id of the frame element.
Returns: driver focused on the given frame (current frame)
Throws: NoSuchFrameException - If the frame is not found

Below is the example code snippet using frame name.

public void switchToFrame(String frame) { try { driver.switchTo().frame(frame); System.out.println("Navigated to frame with name " + frame); } catch (NoSuchFrameException e) { System.out.println("Unable to locate frame with id " + frame + e.getStackTrace()); } catch (Exception e) { System.out.println("Unable to navigate to frame with id " + frame + e.getStackTrace()); } }

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

driver.switchTo().frame(WebElement frameElement);

Select a frame using its previously located WebElement.
Parameters: frameElement - The frame element to switch to.
Returns: driver focused on the given frame (current frame).
Throws: NoSuchFrameException - If the given element is neither an iframe nor a frame element. And StaleElementReferenceException - If the WebElement has gone stale.

Below is the example code to send an Element to the and switch.

public void switchToFrame(WebElement frameElement) { try { if (isElementPresent(frameElement)) { driver.switchTo().frame(frameElement); System.out.println("Navigated to frame with element "+ frameElement); } else { System.out.println("Unable to navigate to frame with element "+ frameElement); } } catch (NoSuchFrameException e) { System.out.println("Unable to locate frame with element " + frameElement + e.getStackTrace()); } catch (StaleElementReferenceException e) { System.out.println("Element with " + frameElement + "is not attached to the page document" + e.getStackTrace()); } catch (Exception e) { System.out.println("Unable to navigate to frame with element " + frameElement + e.getStackTrace()); } }

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

Some times when there are multiple Frames (Frame in side a frame), we need to first switch to the parent frame and then we need to switch to the child frame. below is the code snippet to work with multiple frames.

public void switchToFrame(String ParentFrame, String ChildFrame) {
		try {
			driver.switchTo().frame(ParentFrame).switchTo().frame(ChildFrame);
			System.out.println("Navigated to innerframe with id " + ChildFrame
					+ "which is present on frame with id" + ParentFrame);
		} catch (NoSuchFrameException e) {
			System.out.println("Unable to locate frame with id " + ParentFrame
					+ " or " + ChildFrame + e.getStackTrace());
		} catch (Exception e) {
			System.out.println("Unable to navigate to innerframe with id "
					+ ChildFrame + "which is present on frame with id"
					+ ParentFrame + e.getStackTrace());
		}
	}

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

After working with the frames, main important is to come back to the web page. if we don't switch back to the default page, driver will throw an exception. Below is the code snippet to switch back to the default content.

public void switchtoDefaultFrame() {
		try {
			driver.switchTo().defaultContent();
			System.out.println("Navigated back to webpage from frame");
		} catch (Exception e) {
			System.out
					.println("unable to navigate back to main webpage from frame"
							+ e.getStackTrace());
		}
	}




No comments:

Post a Comment

Note: Only a member of this blog may post a comment.