Switching Frames in WebDriver

In WebDriver there is no direct replacement of the Selenium IDE command selectFrame, however there is a fairly straightforward approximation by using the driver.SwitchTo() method. This method allows you to change the focus to different windows, alerts and frames, but we will be focussing on frames in this post. Below are examples on how to switch to a frame by using its ID, its parent frame, or the top level.

To select the frame by specifying its ID, replace

 selectFrame | frmFrameID | 

With:

IWebElement frmDashboard = null;
frmDashboard = driver.FindElement(By.Id("frmDashboard"));
driver.SwitchTo().Frame(frmDashboard);

To select the top level frame, replace

 selectFrame | relative=top | 

with the following line

 driver.SwitchTo().DefaultContent();

And finally to select a frame by the current frame’s parent frame, replace

selectFrame | relative=up | 

with the following line

 driver.SwitchTo().ParentFrame();

In later posts I will expand on the other actions available to us with driver.SwitchTo();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s