Python + Selenium notes (9): Operation warning and pop-up box, pythonselenium

Source: Internet
Author: User

Python + Selenium notes (9): Operation warning and pop-up box, pythonselenium

(1)Preface

Developers use JavaScript warnings or modal dialogs to prompt for verification error information, alarm information, returned information after the operation is executed, and even to receive input values.

(2)AlertClass

Selenium WebDriver uses the Alert class to manipulate JavaScript warnings.

(3)AlertFunctions and Methods

Function/attribute

Simple Description

Text

Obtain the prompt information (text content) in the pop-up window)

Alert. text

 

Method

Simple Description

Accept ()

Receive JS warning information and click OK

dismiss()

Accept JS warning information and click Cancel

send_keys(value)

Input information to the simulation Element

 

(4)Webdriver APIAdditional instructions (based on note 7)

Method

Simple Description

Switch_to_active_element ()

Returns the object of the current focus.

Switch_to_alert ()

Switch the focus to the warning displayed on the current page

Switch_to_default_content ()

Switch the focus to the default framework

Switch_to_frame ()

Switches the focus to the specified frame through the index, name, and webpage Element

Switch_to_window ()

Switch focus to the specified window

 

(5)Example (check whether a prompt is displayed when you click to exit on the blog homepage)

1 import unittest 2 from selenium import webdriver 3 class SignOut (unittest. testCase): 4 5 @ classmethod 6 def setUpClass (cls): 7 # Get the custom profile path 8 cls. profile = webdriver. firefoxProfile \ 9 (r 'C: \ Users \ quanhua \ AppData \ Roaming \ Mozilla \ Firefox \ Profiles \ tnwjkr4m. selenium ') 10 # create a Firefox browser driver instance and load the custom profile11 cls at startup. driver = webdriver. firefox (cls. profile) 12 cls. driver. implicitly_wait (20) 13 cls. driver. maxim Ize_window () 14 cls. driver. get ('https: // www.cnblogs.com/') 15 def test_sign_out (self): 16 driver = self. driver17 # locate exit 18 span_userinfo = driver on the blog homepage. find_element_by_css_selector ('# span_userinfo') 19 sign_out_btn = quit ('logout ') 20 sign_out_btn.click () 21 # switch the focus to the warning popped up on the current page, and get text22 alert = driver in the pop-up box. switch_to_alert () 23 alert_text = alert. text24 # Check whether the message is displayed when you click to exit. Are you sure you want to exit? 25 self. assertTrue (alert_text = 'Are you sure you want to exit? ') 26 alert. accept () # click OK 27 28 @ classmethod29 def tearDownClass (cls): 30 cls. driver. quit ()

 

(6)To use the sample code, you need to configure the Firefox profile (for example)

Or Baidu Python selenium profile

After the configuration, select the custom profile to open Firefox and log on to the blog site once (Select Automatic Logon). The sample code will run.

 

 

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.