1. 程式人生 > >java selenium (十一) 操作彈出對話框

java selenium (十一) 操作彈出對話框

exce key gif lee target his @value put java se

對話框類型

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("//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("//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("//input[@value=‘confirm‘]"));
        confirmButton.click();
        Thread.sleep(2000);
        Alert javascriptConfirm = driver.switchTo().alert();
        javascriptConfirm.accept();
        Thread.sleep(2000);
        javascriptConfirm = driver.switchTo().alert();
        javascriptConfirm.accept();
    }
技術分享

java selenium (十一) 操作彈出對話框