Selenium2+python Automation 16-alert\confirm\prompt "Reprint"

Source: Internet
Author: User

Objective

Not all pop-up boxes are called alert, and before using the alert method, you need to identify whether it is alert or not. First recognize what the CHU alert looks like, the next encounter, you can use the corresponding method to solve.

The main methods for Alert\confirm\prompt popup operations are:

Text: Get text value

Accept (): Click "Confirm"

Dismiss (): Click "Cancel" or cross out the dialog box

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


I. Understanding ALERT\CONFIRM\PROMPT

1. For example, from the top to the bottom of the alert\confirm\prompt, the first to recognize what looks like, later encountered on the know how to operate.

2.html source code as follows (interested can copy out, copied into the txt text, the suffix is changed to HTML, and then opened with a browser)

<title>Alert</title>

<body>

<input id = "alert" value = "alert" type = "button" onclick = "alert (' Have you followed Yoyoketang?"). ‘);" />

<input id = "Confirm" value = "confirm" type = "button" onclick = "confirm (' OK concern public number: Yoyoketang? ‘);" />

<input id = "Prompt" value = "prompt" type = "button" onclick = "var name = prompt (' Please enter public number: ', ' Yoyoketang '); document.write (name) "/>

</body>


Second, alert operation

1. Use the Switch_to_alert () method to switch to the alert pop-up box first

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

3.accept () Click the Confirm button

4.dismiss () corresponds to the upper right corner of the dot x, cancels the popup box

(The path of the URL, directly copying the path opened by the browser)


 

Third, confirm operation

1. Use the Switch_to_alert () method to switch to the alert pop-up box first

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

3.accept () Click the Confirm button

4.dismiss () corresponds to the Point Cancel button or the upper right corner of the point x, cancel the popup box

(The path of the URL, directly copying the path opened by the browser)


 

Four, prompt operation

1. Use the Switch_to_alert () method to switch to the alert pop-up box first

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

3.accept () Click the Confirm button

4.dismiss () corresponds to the upper right corner of the dot x, cancels the popup box

5.send_keys () There are multiple input boxes where you can enter text content with the Send_keys () method

(The path of the URL, directly copying the path opened by the browser)?


 

V. The pits encountered by select

1. In the operation of Baidu settings inside, click on the "Save Settings" button, the alert pop-up box does not bounce out. (ie browser is possible)

2. Analysis reason: After slowly debugging found, when the "Save Settings" button, because of the previous select operation, lost focus

3. Workaround: After the select operation, do a click () operation


 

s = driver.find_element_by_id ("NR")

Select (s). Select_by_visible_text ("Show 20 per page")

Time.sleep (3)

S.click ()



VI. 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)

# Mouse moves to "settings" button

Mouse = Driver.find_element_by_link_text ("Settings")

Actionchains (Driver). Move_to_element (mouse). Perform ()

Driver.find_element_by_link_text ("Search Settings"). Click ()

# through Text:select_by_visible_text ()

s = driver.find_element_by_id ("NR")

Select (s). Select_by_visible_text ("Show 20 per page")

Time.sleep (3)

S.click ()

Driver.find_element_by_link_text ("Save Settings"). Click ()

Time.sleep (5)

# Get Alert Bullet Box

t = Driver.switch_to_alert ()

Print T.text

T.accept ()


This should be relatively simple, alert related content is relatively small, although some pages also have pop-up windows, but not all pop-up windows are called alert. Alert Popup Box interface is relatively concise, is called the system pop-up warning box, no flashy things, or very easy to distinguish.

Selenium2+python Automation 16-alert\confirm\prompt "Reprint"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.