Objective
Some pages of the link opens, will reopen a window, in this case, want to work on the new page, you have to switch the window first. The Get window's unique identity is represented by a handle, so just switch the handle so we can operate flexibly on multiple pages.
To open the Baidu News Page Search button on the link page for example, open each button in turn, and verify the test results. Use the script to bulk operations, you can reduce duplication of work, repeat things let the script to execute it!
First, positioning a set of elements
1. Open the Baidu News page: http://news.baidu.com/
2. Locate a row of buttons on the Search box page, paste, know, etc.
3. Positioning a set of element methods in the eighth has been said, here is not much to say, through Firebug can see they have the common attribute tag is a, and there is a dad: <div class= "tab" >
Second, get the current window handle
1. When you click on the "Page" button on the Baidu News page, a new window will open.
2. Man-made operation, you can switch to a different window by clicking the window, but the script does not know which window you want to manipulate, this time only can get the window unique identity: Handle
3. Get the handle of the current page: Driver.current_window_handle
1470719326447960.png903x456 15.6 KB
Three, get all handles
1. First navigate to all buttons via CSS: ". Tab>a"
2. Take a single click from a set of positions to open
3. Then get all the handles of the current page and find that there are two at this time
Iv. Toggle Handle
1. Cycle judgment is equal to the first page handle
2. If not, the description is a handle to the new page
3. After getting the new page handle, you can switch to the newly opened page
4. Print the title of the new page to see if the switch was successful
Close the new window and cut back to the homepage
1. After opening the new page, actually just want to verify the new page jump right, here can do a simple verification, get the current page title verification
2. Close the new window after the verification is finished
3. Cut back the handle to the home page
4. Print the handle of the current page to see if it switches to the homepage.
Six, bulk operation
1. Put the results of the required calibration in the list queue R
2.for loop Traversal performs each button's Click action
3. The result of the decision corresponds to each result in the list
Vii. Final Script
1. After finishing the final script as follows, the result
# Coding:utf-8
From selenium import Webdriver
Import time
Driver = Webdriver. Firefox ()
Driver.get ("http://news.baidu.com/")
Driver.implicitly_wait (10)
# Gets the current window handle
H = Driver.current_window_handle
# Locate Web pages, paste bars and other links
s = Driver.find_elements_by_css_selector (". Tab>a")
R = [u "Baidu", U "Paste", U "Know", U "Music",
U "Picture", U "video", U "map", U "Encyclopedia", U "Library"]
For a, b in Zip (S, r):
A.click ()
Text = A.text
Time.sleep (2)
All_h = Driver.window_handles
# Loop to determine if it is equal to the first page handle
For I in All_h:
If I! = h:
Driver.switch_to.window (i)
Time.sleep (1)
Print Driver.title
If B in Driver.title:
Print (text+u "page open OK")
Else
Print (text+u "page test Failed")
Driver.close () # Close the current page
Driver.switch_to.window (h) # Tangent handle to home page
Driver.quit
Selenium2+python Automation 13-Multiple windows, handles (handle)