Web 開發(fā)人員通常需要利用JavaScript彈出對話框來給用戶一些信息提示, 包括以下幾種類型
閱讀目錄
對話框類型
1. 警告框: 用于提示用戶相關信息的驗證結果, 錯誤或警告等

2. 提示框: 用于提示用戶在當前對話框中輸入數(shù)據(jù),一般需要用戶單擊取消或者確認按鈕

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(); }以上就是對 java selenium操作彈出對話框的資料整理,后續(xù)繼續(xù)補充,謝謝大家對本站的支持!
新聞熱點
疑難解答