Selenium alert pop-up processing

Time:2021-11-2

There are three types of page pop ups:

  1. Alert (warning message)
  2. Confirm
  3. Prompt (prompt)

Selenium provides the following methods for the alert pop-up window on the page:

Serial number Method / attribute describe
1 accept() accept
2 dismiss() cancel
3 text Gets the displayed text
4 send_keys() Input content

correspondingTreatment method

  1. Alert: webdriver.switch_ to.alert.accept()
  2. Confirm:

    1. WebDriver.switch_to.alert.accept()
    2. WebDriver.switch_to.alert.dismiss()
  3. Prompt: webdriver.switch_ to.alert.send_ keys()

Alert pop-up

The alert pop-up box is used to prompt the notification information. You only need to click OK after reading it.

from selenium import webdriver
from time import sleep

Driver = webdriver. Chrome() # open browser
driver.get(" http://sahitest.com/demo/alertTest.htm ") # jump to test page
sleep(1)
element = driver.find_ element_ by_ Name ("B1") # location
Element. Click() # Click
sleep(1)
alert = driver.switch_ To.alert # switch to pop-up
Print (alert. Text) # print the information displayed in the pop-up window: alert message
Alert. Accept() # accept
sleep(2)

Driver. Quit() # close browser

Confirm pop-up

The confirm pop-up box mainly allows the user to determine whether to perform an operation. For example, Taobao, jd.com, etc. deleting an order is a pop-up that allows the user to determine whether to delete it and avoid misoperation.

The confirm pop-up box provides two options, “confirm” or “Cancel”. Just select one of them:

Confirmation: webdriver.switch_ to.alert.accept()

Cancel: webdriver.switch_ to.alert.dismiss()

from selenium import webdriver
from time import sleep

Driver = webdriver. Chrome() # open browser
driver.get(" http://sahitest.com/demo/confirmTest.htm ") # jump to test page
sleep(1)
element = driver.find_ element_ by_ Name ("B1") # location
#1. Acceptance
Element. Click() # Click
sleep(1)
alert = driver.switch_ To.alert # switch to pop-up
Print (alert. Text) # print the information displayed in the pop-up window: alert message
Alert. Accept() # accept
sleep(2)

#2. Cancel
Element. Click() # Click
sleep(1)
alert = driver.switch_ To.alert # switch to pop-up
Print (alert. Text) # print the information displayed in the pop-up window:
alert.dismiss()
sleep(2)

Driver. Quit() # close browser

Prompt pop-up box

Prompt pop-up box is used to require the user to input information before submitting. Selenium provides input information by:

WebDriver.switch_to.alert.send_keys()

from selenium import webdriver
from time import sleep

Driver = webdriver. Chrome() # open browser
driver.get(" http://sahitest.com/demo/promptTest.htm ") # jump to test page
sleep(1)
element = driver.find_ element_ by_ Name ("B1") # location
#1. Acceptance
Element. Click() # Click
sleep(1)
alert = driver.switch_ To.alert # switch to pop-up
Print (alert. Text) # print the information displayed in the pop-up window: alert message
alert.send_ Keys ("automated test") # input
sleep(1)
Alert. Accept() # accept
sleep(2)

Driver. Quit() # close browser

be careful: some pop ups are not the alert window of the browser, buthtml element , for this dialog box, you only need to select it through the selector described earlier and perform corresponding operations.

summary

Selenium alert pop-up processing