Selenium的弹窗处理

Selenium的弹窗处理!

对话框相信大家都不陌生,常见的对话框为三种alert、confirm、prompt。这些对话框对Selenium来说不算是界面层的东西。因为他是JS做的。看到这是不是想到了利用之前的JS代码了?不过Selenium解决了这个问题,给我们提供了三个基本方法:accept();、dismiss();、sendKeys();、等方法。这三个分别对应确认、取消、输入。通过这三个方法我们可以实现对JS对话框进行操作。



import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class alertConfirmPromptDemo {

	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
		WebDriver driver=new ChromeDriver();
		Dimension di=new Dimension(600, 600);
		driver.manage().window().setSize(di);
		driver.get("file:///D:/wb-zq379240/eclipse-workspace/softlyDemo/htmlDemo/demo.html");
//		alert弹窗处理	
		
//		driver.findElement(By.id("alert")).click();
//		driver.switchTo().alert().accept(); //点击确认

//		//confirm弹窗处理	
//		driver.findElement(By.id("confirm")).click();
//		driver.switchTo().alert().dismiss();   //点击确认
//		Thread.sleep(2000);
//		driver.findElement(By.id("confirm")).click();
//		driver.switchTo().alert().accept();   //点击取消

//		//promot弹窗处理	
//		driver.findElement(By.id("promot")).click();  
//		System.out.println(driver.switchTo().alert().getText());//获取弹窗提示文本
//		driver.switchTo().alert().sendKeys("你狠棒棒");  //想窗口输入文本
//		driver.switchTo().alert().accept();  //点击确认
		
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章