Location>code7788 >text

Selenium popup handling

Popularity:742 ℃/2024-10-29 22:05:42

There are three types of pop-up boxes in Selenium, and this article describes the methods for handling them

I. Selenium three kinds of pop-up boxes

alert: Used for alerting, displaying a warning box with the specified message and confirmation button.
confirm: Used for confirmation, displays a dialog box with the specified message and OK and Cancel buttons.
prompt: Used for user input to display a dialog box where input can be made.
These three kinds of pop-up boxes are not html page elements, but javascript controls, so you can not use the traditional method to operate, you need to use another method of operation, the following three pop-up boxes have been introduced to deal with the method of pop-up box properties:
(1) accept(): used to confirm, applicable to three kinds of pop-up boxes
(2) dismiss(): used to cancel, applies to three kinds of pop-up boxes
(3) send_keys(): only applies to the prompt method, used to enter text
(4) text: used to get the text value of the tip

Second, the definition of the form form

Define three hyperlinks and click on them to bring up different pop-up boxes.

<a href="javascript:alert('alert box')" >Alert</a><br>
<a href="javascript:confirm('Confirm Delete?')" >Confirm</a><br>

Prompt returns the content of the variable age, and writes the variable to the page after fetching the return value.
<a href="javascript:var age=prompt('Please enter your age');(age)" >Prompt</a><br>

The interface is as follows

III. Form testing

1、alert test

(1) Use case 1: click on the hyperlink alert
Result 1: pop-up alert type pop-up window
image
Automation code: directly call the click() method in the WebElement class.

.find_element(, "alert").click()#Click the hyperlink alert

(2) Use case 2: click OK in the pop-up box
Result 2: Pop-up box disappears
image

Automation code:
First of all, you need to cut the scope of the Webdriver from the main window to the pop-up box, the operation of the three kinds of pop-up boxes: alert, confirm, prompt before, you need to perform the operation, the method used is .switch_to.alertIn the second step, you can operate in the pop-up box and click OK, using the method accept().

alert=.switch_to.alert#Switch to alert from the main window and return an alert object.
sleep(2)
#print()
()#Click OK
sleep(2)
2. Confirm test

(1) Use case 1: click on the hyperlink confirm
Result 2: Confirm type popup box
image

Automation code: the same direct call to the click() method in the WebElement class

.find_element(, "confirm").click()#Click the confirm hyperlink

(2) Use Case 2: Click OK
Result 2: Pop-up box disappears
image
Automation code:
The method used is accept().

confirm=.switch_to.alert#Switch to confirm popup box
sleep(2)
print()#Print the text in the popup window
()#Click OK
sleep(2)

(3) Use case 3: Click to cancel
Result 3: Pop-up box disappears
image
Automation code: the method used is dismiss()

()#Click to cancel
sleep(2)
3、prompt test

(1) Use case 1: click on the hyperlink prompt
Result 1: Prompt type popup box appears
image

Automation code: directly call the click() method in the WebElement class

.find_element(,"prompt").click()

(2) Use case 2: Entering and confirming content
Use case 2: text is successfully entered and written to the page
image

image

Automation code:
The send_keys() method is used to enter text, and the accept() method is used to click OK.

prompt=.switch_to.alert#Switch to prompt popup box
sleep(2)
prompt.send_keys('12')
sleep(2)
()
sleep(2)

III. Master code

Click to view code
from selenium import webdriver
from time import sleep
import os
from import By

class TestCase(object):
    def __init__(self):
        =()
        path=((__file__))
        file_path="file:///"+path+"/"
        (file_path)

    def test_alert(self):
        .find_element(,"alert").click()#strike (on the keyboard)alerthyperlink, link
        alert=.switch_to.alert#Switch to pop-up window,Returns an object
        sleep(2)
        print()#PrintabletextContent determination
        ()#strike (on the keyboard)确定
        sleep(2)

    def test_confirm(self):
        .find_element(,"confirm").click()#strike (on the keyboard)confirmhyperlink, link
        confirm=.switch_to.alert#Switch toconfirmname of "pop-up" radical in Chinese characters (Kangxi radical 22)
        sleep(2)
        print()#Print the text in the popup window
        ()#strike (on the keyboard)确定
        sleep(2)
        ()#strike (on the keyboard)取消
        sleep(2)

    def test_prompt(self):
        .find_element(,"prompt").click()
        prompt=.switch_to.alert#Switch topromptname of "pop-up" radical in Chinese characters (Kangxi radical 22)
        sleep(2)
        prompt.send_keys('12')
        sleep(2)
        ()
        sleep(2)

if __name__=="__main__":
    case=TestCase()
    #case.test_alert()
    #case.test_confirm()
    case.test_prompt()