亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

java selenium工具 操作彈出對話框分享

標簽:
Java

Web 开发人员通常需要利用JavaScript弹出对话框来给用户一些信息提示, 包括以下几种类型:


对话框类型

1.  警告框: 用于提示用户相关信息的验证结果, 错误或警告等

2. 提示框: 用于提示用户在当前对话框中输入数据,一般需要用户单击取消或者确认按钮

3. 确认框: 用于提示用户确认或者取消某个操作,一般需要用户单击取消或者确认按钮

 

测试页面

用如下页面为例进行讲解,  包括了警告框,提示框,确认框

http://sislands.com/coin70/week1/dialogbox.htm

 

 

Selenium 操作对话框的代码

public static void testAlert(WebDriver driver)
    {
        String url="http://sislands.com/coin70/week1/dialogbox.htm";
        driver.get(url);
        
        WebElement alertButton = driver.findElement(By.xpath("http://input[@value='alert']"));
        alertButton.click();
        
        Alert javascriptAlert = driver.switchTo().alert();
        System.out.println(javascriptAlert.getText());
        javascriptAlert.accept();
    }    
    public static void testPrompt(WebDriver driver) throws Exception
    {
        String url="http://sislands.com/coin70/week1/dialogbox.htm";
        driver.get(url);
        
        WebElement promptButton = driver.findElement(By.xpath("http://input[@value='prompt']"));
        promptButton.click();
        Thread.sleep(2000);
        Alert javascriptPrompt = driver.switchTo().alert();
        javascriptPrompt.sendKeys("This is learning Selenium");
        javascriptPrompt.accept();    
        
        System.out.println(javascriptPrompt.getText());
        
        javascriptPrompt=driver.switchTo().alert();
        javascriptPrompt.accept();
        
        Thread.sleep(2000);
        promptButton.click();
        javascriptPrompt=driver.switchTo().alert();
        javascriptPrompt.dismiss();
        Thread.sleep(2000);
        javascriptPrompt=driver.switchTo().alert();
        javascriptPrompt.accept();
    }    
    public static void testConfirm(WebDriver driver) throws Exception
    {
        String url="http://sislands.com/coin70/week1/dialogbox.htm";
        driver.get(url);
        
        WebElement confirmButton = driver.findElement(By.xpath("http://input[@value='confirm']"));
        confirmButton.click();
        Thread.sleep(2000);
        Alert javascriptConfirm = driver.switchTo().alert();
        javascriptConfirm.accept();
        Thread.sleep(2000);
        javascriptConfirm = driver.switchTo().alert();
        javascriptConfirm.accept();
    }

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消