There are three types of page pop-ups:

  1. Alert:
  2. Confirm information
  3. Prompt for input

Selenium provides the following methods for the alert pop-up that appears on a page:

The serial number Methods/attributes describe
1 accept() accept
2 dismiss() cancel
3 text Gets the displayed text
4 send_keys() The input

Corresponding processing methods:

  1. Alert: webdriver.switch_to.alert. Accept ()
  2. Confirm information:
    1. WebDriver.switch_to.alert.accept()
    2. WebDriver.switch_to.alert.dismiss()
  3. Prompt: webdriver.switch_to.alert.send_keys ()

Alert A dialog box is displayed

The alert dialog box is displayed for notifying the user. You only need to read the dialog box and click ok.

from selenium import webdriver
from time import sleep

driver = webdriver.Chrome()  # Open browser
driver.get("http://sahitest.com/demo/alertTest.htm")  Jump to the test page
sleep(1)
element = driver.find_element_by_name("b1")  # locate
element.click()  # click
sleep(1)
alert = driver.switch_to.alert  # Switch to popover
print(alert.text)  Print the pop-up Message: Alert Message
alert.accept()  # to accept
sleep(2)

driver.quit()  # Close the browser
Copy the code

The Confirm dialog box is displayed

The Confirm dialog box is displayed to let the user decide whether to perform an operation. For example, taobao, JINGdong, etc., delete order is pop-up such pop-up, let the user determine whether to delete, to avoid user misoperation.

The Confirm dialog box provides two options, Confirm or Cancel. Just choose one of them:

Confirmed: WebDriver. Switch_to. Alert. The accept ()

Cancellation: 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 the test page
sleep(1)
element = driver.find_element_by_name("b1")  # locate
# 1 acceptance
element.click()  # click
sleep(1)
alert = driver.switch_to.alert  # Switch to popover
print(alert.text)  Print the pop-up Message: Alert Message
alert.accept()  # to accept
sleep(2)

# 2. Cancel
element.click()  # click
sleep(1)
alert = driver.switch_to.alert  # Switch to popover
print(alert.text)  # Print the information displayed in the popup:
alert.dismiss()
sleep(2)

driver.quit()  # Close the browser
Copy the code

Prompt dialog box is displayed

Prompt displays a box that requires the user to enter 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 the test page
sleep(1)
element = driver.find_element_by_name("b1")  # locate
# 1 acceptance
element.click()  # click
sleep(1)
alert = driver.switch_to.alert  # Switch to popover
print(alert.text)  Print the pop-up Message: Alert Message
alert.send_keys("Automated testing")  # Input content
sleep(1)
alert.accept()  # to accept
sleep(2)

driver.quit()  # Close the browser
Copy the code

Note: Some pop-ups are not browser alert Windows, but HTML elements. For such dialog boxes, just select them through the selector described earlier and perform the corresponding actions.

conclusion