Python-Identify alert, window, and actions

Source: Internet
Author: User

Selenium's Identification Alert, window, and operationOriginal August 24, 2016 11:01:04
    • 4820
    • 0
    • 2

For more articles on Python selenium, follow my column:python selenium automated test details

Many people can't tell what alert is, what window is, how to identify alert and window in today's discussion, and a dialog box for page elements such as Div masquerading.

1. Distinguish

First, distinguish the alert, window, and Disguise dialog boxes:

    • Alert, browser popup, is generally used to confirm certain actions, enter a simple text or user name, password, etc., depending on the browser, the style of the popup box is different, but it is a very simple small box. It is not possible to get the elements of the box in Firebug, that is, alert is not part of the Web Dom tree. as shown in the following:


    • Windows, browser window, after clicking on a link may open a new browser window, with the previous window is a parallel relationship (alert and window is a parent-child relationship, or a subordinate relationship, alert must rely on a certain window), has its own address bar, maximize, minimize the button and so on. This is easy to tell.

    • div Disguise dialog box, is through the page elements disguised as dialog boxes, this dialog is generally more fancy, the content is more, and with the browser is not a set of, in the Web page with Firebug can get to its elements, such as:

2.alert Operation

The corresponding classes are provided for alert,selenium for processing.

Selenium.webdriver.common.alert. Alert (driver)

All actions for alert are listed first:

Alert(driver).accept()  # 等同于点击“确认”或“OK”Alert(driver).dismiss()  # 等同于点击“取消”或“Cancel”Alert(driver).authenticate(username,password) # 验证,针对需要身份验证的alert,目前还没有找到特别合适的示例页面Alert(driver).send_keys(keysToSend) # 发送文本,对有提交需求的prompt框(3)Alert(driver).text # 获取alert文本内容,对有信息显示的alert框
    • 1
    • 2
    • 3
    • 4
    • 5

Example code:

Example 1:switch_to.alert, accept (), text

Test link http://sahitest.com/demo/alertTest.htm

# -*- coding: utf-8 -*-from selenium import webdriverfrom time import sleepdriver = webdriver.Firefox()driver.maximize_window()driver.get(‘http://sahitest.com/demo/alertTest.htm‘)driver.find_element_by_name(‘b1‘).click()a1 = driver.switch_to.alert # 通过switch_to.alert切换到alertsleep(1)print a1.text # text属性输出alert的文本a1.accept() # alert“确认”sleep(1)driver.quit()
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

Results

Message
    • 1

Example 2:alert (driver), dismiss ()

Test link http://sahitest.com/demo/confirmTest.htm

#-*-coding:utf-8-* -from selenium import webdriver From time import sleepfrom Selenium.webdriver.common.alert Span class= "Hljs-keyword" >import alertdriver = Webdriver. Firefox () Driver.maximize_window () driver.get ( http://sahitest.com/demo/ Confirmtest.htm ') driver.find_element_by_name ( ' B1 '). Click () a1 = Alert (Driver) # direct instantiation of Alert object Sleep (1) print a1.text # the Text property output alert for A1.dismiss () # alert "Cancel" sleep (1) driver.quit ()            
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

Results

Some question?
    • 1

Example 3:switch_to.alert, Send_keys (keystosend)

Test link http://sahitest.com/demo/promptTest.htm

#-*-Coding:utf-8-*-from Selenium Import Webdriverfromtime import sleepdriver = Webdriver. Firefox () Driver.maximize_window () driver.get ( http://sahitest.com/demo/promptTest.htm ') Driver.find_element_by_name ( ' B1 '). Click () a1 = Driver.switch_to.alert # via Switch_to.alert switch to Alertsleep (1) print a1.text # the Text property of the output alert in A1.send_keys (  ' send some words to alert! ') # incoming string prompt 1" A1) . accept () print driver.find_element_by_name ( T1 '). Get_attribute ( ' value ') driver.quit ()     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

Results

Some prompt?send some words to alert!
    • 1
    • 2

The Authenticate (Username,password) method did not find a suitable example Web page, do not write the sample code here, but the usage is the same as Send_keys, but only passed two parameters. And this kind of box seldom appears, do not do more research.

3.window Operation

window is similar to alert, but is parallel to the original window, so it is only necessary to switch to the new window to manipulate the element of the window.

Driver.switch_to. window (window_handle)

window is positioned through the window handle, handle. Instead, selenium provides two property methods to query the window handle:

Driver.current_window_handle
Driver.window_handles

Using the above two properties to get to the current window and all the handles of the window, you can switch to the other window.

Example code:

Test link http://sahitest.com/demo/index.htm

#-*-Coding:utf-8-*-from SeleniumImport Webdriverfrom TimeImport Sleepfrom Selenium.webdriver.common.alertImport Alertdriver = Webdriver. Firefox () Driver.maximize_window () Driver.get (' http://sahitest.com/demo/index.htm ') Current_window = Driver.current_window_handle# Gets the current window handle Namedriver.find_element_by_link_text (' Window Open Test with Title '). Click () all_windows = Driver.window_handles# get all Windows handle Name# Toggle window, if window is not the current window, Then switch to the Windowfor window in all_windows: if window! = current_window:driver.switch_to. window (window) print Driver.title # print this page titledriver.close () driver.switch_to. Window (Current_window) # close the new window and then cut back to the original window, where you do not cut back to the original window, is unable to manipulate the original window element, even if you close the new window print driver.title # Print original page titledriver.quit ()    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25

Results:

With TitleSahi Tests
    • 1
    • 2

Here are some things to note:

    1. You can only switch windows through the name of window, but window's name is not what you think of as Windows title, but a string of strings, such as ' {976AE257-19BE-4B32-A82E-4BA5063ED0A2} ', so you use the title, URLs and stuff like that, it's impossible to cut windows.
    2. After closing the new window, driver does not automatically jump back to the original window, but requires you to switch back, directly to the location of the window element will be reported nosuchelementexception
3.div Class dialog box

The Div class dialog is a common Web page element that can be positioned and manipulated through normal find_element. Not to be detailed here. Note Set a certain waiting time, so as not yet loaded out to proceed to the next operation, resulting in nosuchelementexception error.

Copyright NOTICE: This article is for bloggers original articles, reproduced please declare the source and add the original link.Report
    • Label:
    • Selenium/
    • Alert/
    • Window/
    • Python/
    • Automated testing
    • This article has been included in the following columns:
    • Python Selenium Automated Test details

Python-Identify alert, window, and actions

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.