How to work with the Elements that are inside the iFrame

If you want to work with the Elements that are inside the iframes. you cannot just inspect that element and do some operations.

For working with the elements that are inside the iframe. First you have to inspect that frame and once we are done with that, we can switch onto that Frame. After switching to that frame we can work with the elements that are inside theFrame.

Follow the below example

1 ) Click on the below to link
https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select

you will be redirected to the above website.
There is a Dropdown on the page and that dropdown is inside the frame.

2) First we will check whether there is a iFrame on the page
Right click on the dropdown, we will get a option that says reload frame it means that the frame is available on the page.
as shown below

3) Now first we will inspect that frame.
you can inspect the frame and now switch to that frame using Id,name,webelement

4) as shown above we can inspect the above frame by id i.e iframeResult

5) Now switching to that frame using below method
driver.switchTo.frame(id)
driver.switchTo.frame( iframeResult )

6) After switching to that frame. We can work with its elements

7) Now inspecting DropDown and selecting its value i.e Audi

Below is the complete code

public class FrameExample{

public static void main(String[] args) throws InterruptedException {

    System.setProperty("webdriver.chrome.driver",
            "Driver path");

    ChromeDriver driver = new ChromeDriver();

    driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select");

    driver.switchTo().frame("iframeResult");

    Select dropdown = new Select(driver.findElement(By.xpath("/html/body/select")));

    dropdown.selectByValue("audi");

    driver.switchTo().defaultContent();

    Thread.sleep(5000);

    driver.close();
}
}

Design a site like this with WordPress.com
Get started