Selenium2+python automation 16-alert\confirm\prompt

foreword   

Not all pop-up boxes are called alerts. Before using the alert method, you must first identify whether it is an alert. First recognize what the alert looks like, and next time you encounter it, you can use the corresponding method to solve it.

The main methods of alert\confirm\prompt pop-up box operations are:

text: get the text value

accept() : Click "Confirm"

dismiss() : Click "Cancel" or fork off the dialog

send_keys() : input text value -- prompt only, no input box on alert and confirm


1. Get to know alert\confirm\prompt

     1. As shown in the figure below, from top to bottom, it is alert\confirm\prompt. First recognize what it looks like, and then you will know how to operate it when you encounter it.

 

    2. The html source code is as follows (if you are interested, you can copy it out, copy it into the txt text, change the suffix to html, and then open it with a browser)

<html>  

<head>  

   <title>Alert</title>  

    </head>  

<body>  

<input id = "alert" value = "alert" type = "button" onclick = "alert('Did you follow yoyoketang?');"/>  

<input id = "confirm" value = "confirm" type = "button" onclick = "confirm('Are you sure to follow WeChat public account: yoyoketang?');"/>  

<input id = "prompt" value = "prompt" type = "button" onclick = "var name = prompt('Please enter the WeChat official account:','yoyoketang'); document.write(name) "/>    

</body>   

</html>  


Second, the alert operation

    1. First use the switch_to_alert() method to switch to the alert pop-up box

    2. You can use the text method to get the pop-up text information

    3.accept() Click the confirm button

    4.dismiss() is equivalent to clicking the x in the upper right corner to cancel the pop-up box

(The path of the url, directly copy the path opened by the browser)

Three, confirm operation

   1. First use the switch_to_alert() method to switch to the alert pop-up box

    2. You can use the text method to get the pop-up text information

    3.accept() Click the confirm button

    4.dismiss() is equivalent to clicking the cancel button or clicking the x in the upper right corner to cancel the pop-up box

(The path of the url, directly copy the path opened by the browser)

Fourth, prompt operation

   1. First use the switch_to_alert() method to switch to the alert pop-up box

    2. You can use the text method to get the pop-up text information

    3.accept() Click the confirm button

    4.dismiss() is equivalent to clicking the x in the upper right corner to cancel the pop-up box

    5.send_keys() There are multiple input boxes here, you can use the send_keys() method to input text content

(The path of the url, directly copy the path opened by the browser)​

Five, select the pit encountered

    1. In the operation of Baidu settings, when you click the "Save Settings" button, the alert pop-up box does not pop up. (Ie browser is ok)

    2. Analysis of the reason: After slow debugging, it was found that when the "Save Settings" button was clicked, the focus was lost due to the previous selection operation.

    3. Solution: After the select operation, make a click() click operation

s = driver.find_element_by_id("nr")

Select(s).select_by_visible_text("Display 20 items per page")

time.sleep(3)

s.click()



6. Final code

# coding:utf-8

from selenium import webdriver

from selenium.webdriver.common.action_chains import ActionChains

from selenium.webdriver.support.select import Select

import time

driver = webdriver.Firefox()

url = "https://www.baidu.com"

driver.get(url)

driver.implicitly_wait(20)

# Move the mouse to the "Settings" button

mouse = driver.find_element_by_link_text("设置")

ActionChains(driver).move_to_element(mouse).perform()

driver.find_element_by_link_text("Search Settings").click()

# By text:select_by_visible_text()

s = driver.find_element_by_id("nr")

Select(s).select_by_visible_text("Display 20 items per page")

time.sleep(3)

s.click()

driver.find_element_by_link_text("Save settings").click()

time.sleep(5)

# Get the alert popup

t = driver.switch_to_alert()

print t.text

t.accept()


This article should be relatively simple. Alert-related content is relatively small. Although some pages also have pop-up windows, not all pop-up windows are called alerts. The interface of alert's pop-up box is relatively simple. It is a system pop-up window warning box called. It is easy to distinguish it without any fancy things.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325343311&siteId=291194637