Selenium python (11) Alert/confirm/prompt Processing (popup box in JS)

Source: Internet
Author: User

The alert, confirm, and prompt generated by JS are processed in Webdriver, and the Switch_to_alert () method is used to locate the alert/confirm/prompt. Then use Text/accept/dismiss/send_keys to operate

①text: Returns the text information in the Alert/confirm/prompt

②accept: Click the Confirm button

③dismiss: Click the Cancel button

④send_keys: Input value, this alert/confirm/prompt no dialog box can not be used, or will be error

Eg: the settings page of Baidu, click "Save Settings" to play the prompt box after Setup is complete.

=======================================================

#!/usr/bin/python
#-*-Coding:utf-8-*-
__author__ = ' Zuoanvip '

From selenium import Webdriver
Import time

Driver = Webdriver. Firefox ()
Driver.get (' http://www.baidu.com ')


#打开搜索设置
Driver.find_element_by_name (' tj_setting '). Click ()
driver.find_element_by_id (' Sl_1 '). Click ()

#点击保存设置
button = Driver.find_element_by_xpath ('//div[@id = ' Gxszbutton ']/input ')
Button.Click ()

#获取网页上的警告信息
Alert = Driver.switch_to_alert ()

#接收警告信息
Alert.accept ()

#打印警告中的文字信息
Alert.text ()

#点击取消按钮
Alert.dismiss ()

#输入值
Alert.send_keys (' Test ')

Selenium python (11) Alert/confirm/prompt Processing (popup box in JS)

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.