Next-Gen App & Browser
Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

How to handle frames in WebDriver?

In WebDriver, a frame is an HTML document embedded inside another. You can switch between frames using the `switchTo()` method and then perform actions on the elements inside the frame. Once you finish your work in that frame, you can return to the default content by calling `switchTo().`

Here are the steps to handle frames in WebDriver:

  • Identify the frame by its name, index, ID, or web element.
  • Use the switchTo().frame() method to change the current frame.
  • You can perform actions on the elements inside the frame.
  • Use the switchTo().defaultContent() method to return to the default content.

The following example demonstrates how to switch to a frame by using its ID and performing actions on the elements inside of it:

// Assume we have a frame with ID 'frame1'

// Switch to the frame using its ID
driver.switchTo().frame("frame1");

// Perform actions on the elements inside the frame
WebElement element = driver.findElement(By.id("elementInsideFrame"));
element.click();

// Switch back to the default content
driver.switchTo().defaultContent();

It is important to remember that if a frame is nested inside another frame, you will need to switch to each frame in order and then back again in reverse order.

Test your website on 3000+ browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...
ShadowLT Logo

Start your journey with LambdaTest

Get 100 minutes of automation test minutes FREE!!

Signup for free