ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

selenium--alert&confirm&prompt弹窗

2022-05-03 00:02:11  阅读:185  来源: 互联网

标签:prompt confirm -- selenium driver System alert 点击 弹窗


1.alert弹窗

alert弹窗是前端页面中常见的一种弹窗,会弹出一些需要用户确认的信息,只有用户点击确定或者取消才能关闭,selenium中通过switchTo().alert()下的accept() 确认和dismiss()取消就可以模拟用户实现点击。

前端alert弹窗代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>alert弹窗</title>
<script>
function myFunction(){
    alert("弹出一个警告框");
}
</script>
</head>
<body>
<input type="button" onclick="myFunction()" value="点击" />
</body>
</html>

后端代码alert弹窗模拟用户点击确认或者取消:

package com.test.selenium;


import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


/**
 *alert弹窗
 *
 */
public class App 
{
    public static void main( String[] args ) throws InterruptedException{
        System.setProperty("webdriver.chrome.bin", "G:/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        String url = "G:/70yuanQZ/新建文件夹/1.18/013alink.html";
        System.out.printf("访问链接:",url);
        driver.get(url);
        driver.manage().window().maximize();
        Thread.sleep(3000);
        driver.findElement(By.xpath("/html/body/input[@type='button']")).click();
        Thread.sleep(5000);
        //获取alert弹窗对象
        Alert alert = driver.switchTo().alert();
        //点击确认按钮
        //alert.accept();
        //获取弹窗文本
        System.out.println(alert.getText());
        //点击忽略或者取消
        alert.dismiss();        
        driver.quit();
        System.out.println("执行结束");   
    }  
}

 

2.confirm弹窗

confirm弹窗点击后会有确认和取消两个选项,点击确认会出现确定的场景,点击取消会出现取消的场景。

前端confirm代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>confirm弹窗</title>
</head>
<body>

<p>点击按钮,显示确认框。</p>
<button id='btn' onclick="myFunction()">点击</button>
<p id="demo"></p>
<script>
function myFunction(){
    var r=confirm("按下按钮!");
    if (r==true){
        window.alert("你点击了确认按钮");
    }
    else{
        window.alert("你点击了取消按钮");
    }
}
</script>
</body>
</html>

 

后端代码alert弹窗模拟用户点击确认或者取消:

package com.test.selenium;


import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


/**
 *alert弹窗
 *
 */
public class App 
{
    public static void main( String[] args ) throws InterruptedException{
        System.setProperty("webdriver.chrome.bin", "G:/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        String url = "G:/70yuanQZ/新建文件夹/1.18/013alink.html";
        System.out.printf("访问链接:",url);
        driver.get(url);
        driver.manage().window().maximize();
        Thread.sleep(3000);
        driver.findElement(By.id("btn")).click();
        Thread.sleep(5000);
        //获取alert弹窗对象
        Alert alert = driver.switchTo().alert();
       
        //点击确认按钮
        alert.accept();
        Thread.sleep(5000);
        //获取弹窗文本
        System.out.println(alert.getText());
        //点击忽略或者取消
        //alert.dismiss();        
        driver.quit();
        System.out.println("执行结束");   
    }  
}

 

3.prompt提示弹窗

这种弹窗一般提示需要手动输入内容才能点击确认。比较前两个弹窗模拟,提示弹窗需要在使用到sendKeys() 方法,数据相关值,不然会报错。

前端代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>prompt弹窗</title>
</head>
<body>
<button id='btn' onclick="myFunction()">点击</button>
<p id="demo"></p>
<script>
function myFunction(){
    var x;
    var person=prompt("请输入你的名字","");
    if (person!=null && person!=""){
        x="你好,朋友!请称呼我:" + person ;
        document.getElementById("demo").innerHTML=x;
    }
}
</script>

</body>
</html>

后端实现代码:

package com.test.selenium;


import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


/**
 *prompt提示弹窗
 *
 */
public class App 
{
    public static void main( String[] args ) throws InterruptedException{
        System.setProperty("webdriver.chrome.bin", "G:/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        String url = "G:/70yuanQZ/新建文件夹/1.18/013alink.html";
        System.out.printf("访问链接:",url);
        driver.get(url);
        driver.manage().window().maximize();
        Thread.sleep(3000);
        driver.findElement(By.id("btn")).click();
        Thread.sleep(5000);
        //获取alert弹窗对象
        Alert alert = driver.switchTo().alert();
        alert.sendKeys("大白");
        Thread.sleep(3000);
        //获取弹窗文本
        System.out.println(alert.getText());
        //点击确认按钮
        alert.accept();
        Thread.sleep(5000);
        //点击忽略或者取消
        //alert.dismiss();        
        driver.quit();
        System.out.println("执行结束");   
    }  
}

以上三种弹窗换汤不换药,总结起来就是应用switchTo().alert()下的相关方法,最常用的如下:

accept()   点击确认按钮

dismiss()  点击取消按钮,如果有的话

sendKeys()  输入值,这个 alert\confirm 没有对话框就不能用了,不然会报错

getText()  获取弹窗文本

wait(timeout)  设置超时时间

 

标签:prompt,confirm,--,selenium,driver,System,alert,点击,弹窗
来源: https://www.cnblogs.com/momo-nancy/p/16217397.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有