selenium+Java处理JS弹窗

JS弹框出现在页面主要分为3种,第一种是Alert警告弹框,该弹窗只有一个确认按钮;第二种是Confirm确认框,该弹窗有确认和取消按钮;第三种是Prompt确认框,该弹窗可以输入内容。selenium处理JS弹框主要用到了org.openqa.selenium.Alert包,核心操作步骤就是要先切换到弹窗:Alert alert=driver.switchTo().Alert()。其中常用的方法有:accept()接收警告信息或点击确认按钮;dismiss()忽略警告信息或点击取消按钮;getText()获取弹框中的文字信息;sengKeys()在弹框中输入信息等。
1、Alert警告弹框,selenium+Java具体示例代码参考如下:

public class Alert_js {

	public static void main(String[] args) throws InterruptedException {
		System.setProperty("webdriver.chrome.driver", "D:\\Webdriver\\chromedriver.exe");
		WebDriver driver=new ChromeDriver();
		driver.get("https://www.runoob.com/try/try.php?filename=tryjs_alert");
		driver.manage().window().maximize();
		//切换到iframe
		driver.switchTo().frame("iframeResult");
		//点击弹出弹窗
		driver.findElement(By.xpath("//input[@type='button']")).click();
		Thread.sleep(2000);
		//切换到Alert弹窗
		Alert alert=driver.switchTo().alert();
		//点击确定按钮
		alert.accept();
		Thread.sleep(2000);
		//关闭浏览器
		driver.quit();
	}
}

2、Confirm确认框,selenium+Java具体示例代码参考如下:

public class Confirm_js {

	public static void main(String[] args) throws InterruptedException {
	
     System.setProperty("webdriver.chrome.driver", "D:\\Webdriver\\chromedriver.exe");
     WebDriver driver=new ChromeDriver();
     driver.get("https://www.runoob.com/try/try.php?filename=tryjs_confirm");
     //切换iframe
     driver.switchTo().frame("iframeResult");
     //点击弹出Confirm确认框
     driver.findElement(By.xpath("//button[text()='点我']")).click();
     Thread.sleep(1000);
     //切换到Confirm确认框
     Alert alert=driver.switchTo().alert();
     Thread.sleep(1000);
     //点击确认框内的取消按钮
     alert.dismiss();
     Thread.sleep(1000);
     //关闭浏览器
     driver.quit();
	}
}

3、Prompt确认框,selenium+Java具体示例代码参考如下:

public class Prompt_js {

	public static void main(String[] args) throws InterruptedException {
		System.setProperty("webdriver.chrome.driver", "D:\\Webdriver\\chromedriver.exe");
		WebDriver driver=new ChromeDriver();
		driver.get("https://www.runoob.com/try/try.php?filename=tryjs_prompt");
	    //切换iframe
	    driver.switchTo().frame("iframeResult");
	    //点击弹出Prompt确认框
	    driver.findElement(By.xpath("//button[text()='点我']")).click();
	    Thread.sleep(1000);
	    //切换到Prompt确认框
	    Alert alert=driver.switchTo().alert();
	    //在弹窗中输入文字
	    alert.sendKeys("一个测试弹窗");
	    Thread.sleep(1000);
	    //点击确认按钮
	    alert.accept();
	    Thread.sleep(1000);
	    //关闭浏览器
	    driver.quit();
	}
}
JavaSelenium中,如果你想要点击一个非Alert(警告对话框)窗口中的元素,例如在一个表单确认提示或者其他类型的自定义对话框上,通常需要使用`WebDriverWait`和`ExpectedConditions`来等待这个元素变为可用。下面是一个基本步骤: 1. 首先,创建一个`WebDriverWait`实例并设置超时时间,因为Selenium不会自动处理非标准浏览器事件如点击按钮。 ```java WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds); ``` 2. 然后,你可以尝试找到特定的元素,比如确认按钮,通常这种对话框会有明确标识的元素来触发确认操作。例如,假设它有一个ID: ```java WebElement confirmButton = driver.findElement(By.id("confirm-button")); ``` 3. 使用`ExpectedConditions`来检查这个元素是否可见并可交互,再进行点击: ```java wait.until(ExpectedConditions.elementToBeClickable(confirmButton)).click(); ``` 如果目标元素不是标准的DOM元素,而是特定的JavaScript函数返回的结果,你可能需要模拟用户交互的行为,比如通过发送键盘事件(`sendKeys()`方法)配合鼠标点击(`ActionChains` API)。 ```java // 假设点击按钮需要先输入文本 String buttonText = "确定"; driver.findElement(By.id("confirmation-input")).sendKeys(buttonText); // 然后再模拟点击 Actions builder = new Actions(driver); builder.moveToElement(confirmButton).click().perform(); ``` 注意,每个应用的具体情况可能会有所不同,实际操作时需要根据实际情况调整查找策略和条件判断。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦里有阳光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值