Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,
I am beginner in selenium and I am trying learn iframe handling. While working with a demo site, i faced a problem in switching frames.
site: Only Testing: iframe1[^] .
The error showing islike as follows:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"name","selector":"fname"}



I would like to know the different ways to switch between frames, Please help me.

What I have tried:

ublic class DragAndDrop {
		WebDriver driver;
		@Test
		public void function() throws InterruptedException{
			//System.setProperty("webdriver.gecko.driver", "./lib/geckodriver.exe");
			String driverPath = "F:\\EclipseWorkspace\\lib_Treesa\\";
	        System.setProperty("webdriver.chrome.driver", driverPath + "chromedriver.exe");
	        ChromeOptions options = new ChromeOptions();
	        		options.addArguments("--start-maximized");

	        driver = new ChromeDriver(options);
	        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

			driver.navigate().to("http://only-testing-blog.blogspot.in/2015/01/iframe1.html");
			int size = driver.findElements(By.tagName("iframe")).size();//<<Calculates the number of inline frames in webpage>
			System.out.println("total frames"+size);

driver.switchTo().frame("frame1");
			
			driver.findElement(By.xpath(".//*[@id='post-body-3107268830657760720']/div[1]/table/tbody/tr[1]/td[1]/input")).click();
			driver.findElement(By.xpath(".//*[@id='post-body-3107268830657760720']/div[1]/table/tbody/tr[2]/td[2]/input")).click();
			driver.findElement(By.xpath(".//*[@id='post-body-3107268830657760720']/div[1]/table/tbody/tr[3]/td[1]/input")).click();
			driver.findElement(By.xpath(".//*[@id='post-body-3107268830657760720']/div[1]/table/tbody/tr[4]/td[2]/input")).click();
			driver.findElement(By.xpath(".//*[@id='post-body-3107268830657760720']/div[1]/table/tbody/tr[5]/td[1]/input")).click();

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

driver.findElement(By.name("fname")).sendKeys("RAM");
			System.out.println("2222222222222222222");
			//Thread.sleep(100);
			driver.findElement(By.id("text3")).sendKeys("GOPAL");
			//Thread.sleep(100);
			driver.findElement(By.id("check3")).click();
			//Thread.sleep(300);
			driver.findElement(By.id("radio2")).click();
			driver.findElement(By.xpath(".//*[@id='post-body-4292417847084983089']/div[1]/form[1]/input[10]")).click();
			WebElement fileInput = driver.findElement(By.name("uploadfile"));
			fileInput.sendKeys("C:/path/to/file.jpg");
Posted
Updated 12-Jun-17 9:52am
v2

1 solution

Hi,

Though I have tried my be but was unable to find a proper solution for the problem using explicit wait functionality .
Mean wile I propose you a solution using Thread.sleep for the above snippet .

Issue 1: In your code no where it was indicated to switch out from frame1 ,as frame2 not a part of the fram1.

Resolution: I have used
driver.switchTo().defaultContent();
for switching to default or main document where web contents are situated.

Issue 2: After switching to main web page , the code has not given enough time to identify and locate frame2.

Resolution: I have used
Thread.sleep(3000);
where 3000 indicates the dimension of time (milliseconds)

And now your code is working fine.

I have uploaded the image in the following link:
[^]



Code as followed:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class codeproject {

	WebDriver driver;
	@Test
	public void function() throws InterruptedException{
		//System.setProperty("webdriver.gecko.driver", "./lib/geckodriver.exe");
		System.setProperty("webdriver.chrome.driver","D:\\training\\DB_Study_selenium\\class7 _ webdrider\\chromedriver.exe");

		   ChromeDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
		driver.navigate().to("http://only-testing-blog.blogspot.in/2015/01/iframe1.html");
		int size = driver.findElements(By.tagName("iframe")).size();//<<Calculates the number of inline frames in webpage>
		System.out.println("total frames"+size);
		
		
		driver.switchTo().frame("frame1");
		
		driver.findElement(By.xpath(".//*[@id='post-body-3107268830657760720']/div[1]/table/tbody/tr[1]/td[1]/input")).click();
		driver.findElement(By.xpath(".//*[@id='post-body-3107268830657760720']/div[1]/table/tbody/tr[2]/td[2]/input")).click();
		driver.findElement(By.xpath(".//*[@id='post-body-3107268830657760720']/div[1]/table/tbody/tr[3]/td[1]/input")).click();
		driver.findElement(By.xpath(".//*[@id='post-body-3107268830657760720']/div[1]/table/tbody/tr[4]/td[2]/input")).click();
		driver.findElement(By.xpath(".//*[@id='post-body-3107268830657760720']/div[1]/table/tbody/tr[5]/td[1]/input")).click();

		driver.switchTo().defaultContent();
		System.out.println("default content");
		// WebDriverWait wait = new WebDriverWait(driver,30);
		 //wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("frame2"));

	Thread.sleep(5000);
		 System.out.println("Initiate for frame 2");
		driver.switchTo().frame("frame2");
		System.out.println("switch to frame2");
		driver.findElement(By.name("fname")).sendKeys("RAM");
		System.out.println("2222222222222222222");
		//Thread.sleep(100);
		driver.findElement(By.id("text3")).sendKeys("GOPAL");
		//Thread.sleep(100);
		driver.findElement(By.id("check3")).click();
		//Thread.sleep(300);
		driver.findElement(By.id("radio2")).click();
		driver.findElement(By.xpath(".//*[@id='post-body-4292417847084983089']/div[1]/form[1]/input[10]")).click();
		WebElement fileInput = driver.findElement(By.name("img"));
		fileInput.sendKeys("C:/path/to/file.jpg");	
	}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900