如何在selenium webdriver中切换iframe [英] How to switch between iframes in selenium webdriver

查看:106
本文介绍了如何在selenium webdriver中切换iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是selenium的初学者,我正在尝试学习iframe处理。在使用演示站点时,我遇到了切换帧的问题。

site:仅测试:iframe1 [ ^ ]。

显示的错误如下:

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");

推荐答案





虽然我已经尝试了但是无法使用显式等待功能找到问题的正确解决方案。

意思是我建议你使用Thread.sleep解决上述代码片段的解决方案。



问题1:在您的代码中没有指示切换出来的地方来自frame1,因为frame2不是fram1的一部分。



分辨率:我使用过
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();

用于切换到Web内容所在的默认文件或主文档。



问题2:切换到主网页后,代码没有足够的时间来识别和定位frame2。 / b>



分辨率:我使用过

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);

其中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");	
	}
}


这篇关于如何在selenium webdriver中切换iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆