Python+selenium notes (ix): Action warnings and pop-up boxes

Source: Internet
Author: User

#之前发的Driver.switch_to_alert () This sentence can be run through, but will pop up a warning message (this 3.x is not recommended)

change to driver. Switch_to.alert would not have been.

(i) Preface

Developers use JavaScript warnings or modal dialogs to prompt for error messages, alarm information, return information after performing an operation, and even to receive input values.

(ii) Alert class

Selenium Webdriver uses the alert class to manipulate JavaScript warnings.

(iii) Alert functions and methods

Features/Properties

Simple description

Text

Get tips for pop-up windows (text content)

Alert.text

Method

Simple description

Accept ()

Accept the JS warning message and click OK

Dismiss ()

Accept the JS warning message and click Cancel

Send_keys (value)

Simulate input information to an element

(iv) Webdriver API Supplemental Instructions (based on note 7)

Method

Simple description

Switch_to_active_element ()

Returns the object of the current focus

Switch_to_alert ()

Switch focus to the current page pop-up warning

Switch_to_default_content ()

Toggle focus to Default frame

Switch_to_frame ()

Switch focus to the specified frame by index, name, and page element

Switch_to_window ()

Toggle focus to the specified window

(v) Example (check Blog Park Home Click to exit whether pop-up hints)

1 ImportUnitTest2  fromSeleniumImportWebdriver3 classSignOut (unittest. TestCase):4 5 @classmethod6     defSetupClass (CLS):7         #get a custom profile path8Cls.profile =Webdriver. Firefoxprofile9(r'C:\Users\quanhua\AppData\Roaming\Mozilla\Firefox\Profiles\tnwjkr4m.selenium')Ten         #Create a Firefox driver instance and load a custom profile at startup OneCls.driver =Webdriver. Firefox (Cls.profile) ACls.driver.implicitly_wait (20) - Cls.driver.maximize_window () -Cls.driver.get ('https://www.cnblogs.com/') the     deftest_sign_out (self): -Driver =Self.driver -         #Locate the Blog home page exit -Span_userinfo = Driver.find_element_by_css_selector ('#span_userinfo') +SIGN_OUT_BTN = Span_userinfo.find_element_by_link_text ('Exit') - Sign_out_btn.click () +         #switches the focus to the current page pop-up warning, and gets the text of the popup box AAlert =Driver.switch_to_alert () atAlert_text =Alert.text -         #Check Click to exit, do you want to pop up a message confirming exit?  -Self.asserttrue (Alert_text = ='are you sure you want to quit? ') -Alert.accept ()#Click OK -  - @classmethod in     defTeardownclass (CLS): -Cls.driver.quit ()

(vi) Use the sample code to configure Firefox profile (e.g.)

or Baidu Python Selenium profile

Once configured, select the custom profile to open Firefox, log in once to the blog park (tick automatic login), the sample code can be run.

Python+selenium Notes (ix): Action warnings and pop-up boxes

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.