Find elements inside forms and iframe using Java and Selenium WebDriver.

We can find elements inside form and iframe with Selenium webdriver. A form in an html document is represented by a

tag. A form may contain multiple elements inside it. A form can be submitted with a submit() or a click() method.

Let us see a form with fields email and password.

Example

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class FormElements{
   public static void main(String[] args) {
 System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://www.linkedin.com/";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
      // identify elements
      driver.findElement(By.id("session_key")).sendKeys("bde@gmail.com");
      driver.findElement(By.id("session_password")).sendKeys("test!23");
      // submitting form with submit()
      driver.findElement(By.id("session_password")).submit();
      driver.quit()
   }
}

An iframe/ frame is identified in an html document with the

The following methods help to switch between iframes−

  • switchTo().frame(args) – The frame index is put as an argument to the method. The starting index of iframe is 0.

    Syntax −

    driver.switchTo().frame(0), switching to the first iframe.

  • switchTo().frame(args) - The frame name or id is put as an argument to the method.

    Syntax −

    driver.switchTo().frame("nm"), switching to the iframe with name nm.

  • switchTo.frame(args) - The frame webelement is put as an argument to the method.

    Syntax −

    driver.switchTo().frame(f), switching to the iframe with webelement f.

  • switchTo().defaultContent() – To shift focus to the parent page from the iframe.

    Syntax −

    driver.switchTo().defaultContent()

Example

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class iFrameElement{
   public static void main(String[] args) {        System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://the-internet.herokuapp.com/nested_frames";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
      // switch to a iframe
      driver.switchTo().frame("frame-bottom");
      // identify webelement
      WebElement m = driver.findElement(By.cssSelector("body"));
      System.out.println(" The text is: " +m.getText());
      // shift to parent page
      driver.switchTo().defaultContent();
      driver.close();
   }
}
Updated on: 2020-09-18T06:08:24+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements