JavaScript 警示、提示和確認訊息
WebDriver 提供了一個 API,用於處理 JavaScript 提供的三種原生彈出訊息類型。這些彈出視窗由瀏覽器設定樣式,並且自訂程度有限。
警示
其中最簡單的一種稱為警示,它顯示自訂訊息和一個按鈕,該按鈕會關閉警示,在大多數瀏覽器中標記為「確定」。它也可以在大多數瀏覽器中通過按關閉按鈕來關閉,但這將始終執行與「確定」按鈕相同的操作。查看警示範例。
WebDriver 可以從彈出視窗中取得文字,並接受或關閉這些警示。
Alert alert = driver.switchTo().alert();
//Store the alert text in a variable and verify it
String text = alert.getText();
assertEquals(text, "Sample Alert");
//Press the OK button
alert.accept();
element = driver.find_element(By.LINK_TEXT, "See an example alert")
element.click()
wait = WebDriverWait(driver, timeout=2)
alert = wait.until(lambda d : d.switch_to.alert)
text = alert.text
alert.accept()
//Click the link to activate the alert
driver.FindElement(By.LinkText("See an example alert")).Click();
//Wait for the alert to be displayed and store it in a variable
IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());
//Store the alert text in a variable
string text = alert.Text;
//Press the OK button
alert.Accept();
# Store the alert reference in a variable
alert = driver.switch_to.alert
# Get the text of the alert
alert.text
# Press on Cancel button
alert.dismiss
let alert = await driver.switchTo().alert();
let alertText = await alert.getText();
await alert.accept();
//Click the link to activate the alert
driver.findElement(By.linkText("See an example alert")).click()
//Wait for the alert to be displayed and store it in a variable
val alert = wait.until(ExpectedConditions.alertIsPresent())
//Store the alert text in a variable
val text = alert.getText()
//Press the OK button
alert.accept()
確認
確認方塊類似於警示,但用戶也可以選擇取消訊息。查看確認範例。
此範例也示範了儲存警示的不同方法
alert = driver.switchTo().alert();
//Store the alert text in a variable and verify it
text = alert.getText();
assertEquals(text, "Are you sure?");
//Press the Cancel button
alert.dismiss();
element = driver.find_element(By.LINK_TEXT, "See a sample confirm")
driver.execute_script("arguments[0].click();", element)
wait = WebDriverWait(driver, timeout=2)
alert = wait.until(lambda d : d.switch_to.alert)
text = alert.text
alert.dismiss()
//Click the link to activate the alert
driver.FindElement(By.LinkText("See a sample confirm")).Click();
//Wait for the alert to be displayed
wait.Until(ExpectedConditions.AlertIsPresent());
//Store the alert in a variable
IAlert alert = driver.SwitchTo().Alert();
//Store the alert in a variable for reuse
string text = alert.Text;
//Press the Cancel button
alert.Dismiss();
# Store the alert reference in a variable
alert = driver.switch_to.alert
# Get the text of the alert
alert.text
# Press on Cancel button
alert.dismiss
let alert = await driver.switchTo().alert();
let alertText = await alert.getText();
await alert.dismiss();
//Click the link to activate the alert
driver.findElement(By.linkText("See a sample confirm")).click()
//Wait for the alert to be displayed
wait.until(ExpectedConditions.alertIsPresent())
//Store the alert in a variable
val alert = driver.switchTo().alert()
//Store the alert in a variable for reuse
val text = alert.text
//Press the Cancel button
alert.dismiss()
提示
提示類似於確認方塊,不同之處在於它們還包含文字輸入。與使用表單元素類似,您可以使用 WebDriver 的 send keys 來填寫回應。這將完全替換佔位符文字。按下取消按鈕將不會提交任何文字。查看提示範例。
alert = driver.switchTo().alert();
//Store the alert text in a variable and verify it
text = alert.getText();
assertEquals(text, "What is your name?");
//Type your message
alert.sendKeys("Selenium");
//Press the OK button
alert.accept();
element = driver.find_element(By.LINK_TEXT, "See a sample prompt")
driver.execute_script("arguments[0].click();", element)
wait = WebDriverWait(driver, timeout=2)
alert = wait.until(lambda d : d.switch_to.alert)
alert.send_keys("Selenium")
text = alert.text
alert.accept()
//Click the link to activate the alert
driver.FindElement(By.LinkText("See a sample prompt")).Click();
//Wait for the alert to be displayed and store it in a variable
IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent());
//Type your message
alert.SendKeys("Selenium");
//Press the OK button
alert.Accept();
# Store the alert reference in a variable
alert = driver.switch_to.alert
# Type a message
alert.send_keys('selenium')
# Press on Ok button
alert.accept
let alert = await driver.switchTo().alert();
//Type your message
await alert.sendKeys(text);
await alert.accept();
//Click the link to activate the alert
driver.findElement(By.linkText("See a sample prompt")).click()
//Wait for the alert to be displayed and store it in a variable
val alert = wait.until(ExpectedConditions.alertIsPresent())
//Type your message
alert.sendKeys("Selenium")
//Press the OK button
alert.accept()