LoginSignup
30
30

More than 5 years have passed since last update.

Selenium: alert, confirm, promptを扱う方法

Posted at

alertなどが表示された状態でリクエストを行うと下記の例外が発生する

org.openqa.selenium.UnhandledAlertException

その場合は、UnhandledAlertExceptionをキャッチして、alertを閉じることで対処できる:

    try {
      driver.getCurrentUrl // org.openqa.selenium.UnhandledAlertException: unexpected alert open
    } catch {
      case e: org.openqa.selenium.UnhandledAlertException =>
        val alert = driver.switchTo.alert
        alert.accept()
    }

Selenium2でalertなどを操作するときは、AlertオブジェクトをWebDriverから取得する:

val alert = driver.switchTo.alert
  • alertで「OK」を押す
  • confirmで「OK」を押す
  • promptで「OK」を押す
alert.accept()

confirmで「Cancel」を押す

alert.dismiss()

promptで文字列を入力する

alert.sendKeys("テキスト")

alert, confirm, promptに表示された文言を取得する

val text = alert.getText
30
30
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
30