Reference Document: Webdriver official documentation, download link: http://download.csdn.net/detail/kwgkwg001/4004500
Bug Master: "SELENIUM2 Automation Test Practice-based on Python language"
First, control the browser
Webdriver mainly provides methods to manipulate the various elements on the page, but it also provides some ways to manipulate the browser, such as controlling browser size, forward and backward, and so on.
1. Control browser window Size
Webdriver provides the Set_window_size () method to set the browser size:
# Control Browser Size
From selenium import Webdriver
Driver = Webdriver.chrome ("Installation Tool \python\chromedriver.exe")
Driver.get ("http://www.baidu.com")
# parameter numbers are pixel points
Print ("Set browser width 480, height 800 display")
Driver.set_window_size (480,800)
2. Full Screen display
Webdriver provides the Maximize_window () method to make the open browser full-screen, with the same usage as set_window_size ().
3. Control browser forward and backward
The current browser provides both forward and backward functionality while browsing the web, and Webdriver also provides the corresponding forward () and back () methods to simulate the forward and backward buttons:
# control Browser Forward, back
From selenium import Webdriver
Driver = Webdriver.chrome ("Installation Tool \python\chromedriver.exe")
# Visit Baidu Homepage
First_url= ' http://www.baidu.com '
Print ("Now access%s"% (First_url))
Driver.get (First_url)
# Visit the News page
Second_url= ' http://news.baidu.com '
Print ("Now access% s"% (Second_url) ")
Driver.get ("Second_url")
# Back to Baidu homepage
Print ("Back to%s"% (First_url))
Driver.back ()
# forward to the news page
Print ("Forward to%s"% (Second_url))
Driver.forward ()
To see how the script executes, each step prints the current URL address through print (), with the following results:
Now access http://www.baidu.com
Now access http://news.baidu.com
Back to Http://www.baidu.com
Froward to Http://news.baidu.com
4. Simulate the browser before refreshing
In general, we refresh the page through F5 or the Refresh button on the page, Webdriver also provides a Refresh method, refreshing (), to simulate the page refresh:
......
# Refresh the current page
Driver.refresh ()
......
Second, mouse events
In the method provided by the Webelement interface, the mouse click operation can be simulated by clicking (), but there are many ways of mouse interaction, for example: Right click, hover, mouse drag and other functions;
Webdriver provides a actionchains class that encapsulates a common method of mouse operation:
perform (): Performs all the stored behavior in the Actionchains
Context_click (): Right-click
Double_click (): Double-click the mouse
Drag_and_drop (): Mouse drag
move_to_element (): mouse hover
1. Right mouse click operation
# Mouse Right click action
From selenium import Webdriver
From Selenium.webdriver.common.action_chains import Actionchains
Driver = Webdriver.chrome ("Installation Tool \python\chromedriver.exe")
Driver.get ("http://www.baidu.com")
# Navigate to the element you want to right-click
Right_click = driver.find_element_by_id ("id")
# Perform a right-click action on the element you are targeting
Actionchains (Driver). Context_click (Right_click). Perform ()
...
2, mouse hover
Mouse hover pop-up drop-down menu is also a very common function design, move_to_element () method can simulate the mouse hover action, its usage is the same as Context_click ();
# mouse Hover
...
above = driver.find_element_by_id ("id")
Actionchains (Driver). Move_to_element (above). Perform ()
...
3, mouse double-click operation
The Double_click () method is used to simulate the mouse double-click operation;
# Mouse Double-click action
...
Double_click = driver.find_element_by_id ("id")
Actionchains (Driver). Double_click (Double_click). Perform ()
...
4, mouse drag and drop operation
Drag_and_drop (Source,target) hold down the left mouse button on the source element and move to the target element to release;
Source: Drag the mouse from the element
Target: Mouse-released destination element
# Position the location of the element
...
element = driver.find_element_by_id ("id")
# position the target where the element is to be moved
target = driver.find_element_by_id ("xx")
# Perform drag-and-drop operations on elements
Actionchains (Driver). Drag_and_drop (Element,target). Perform ()
...
Three, keyboard events
The keys () class provides almost all the key methods on the keyboard, Send_keys () does not have the ability to simulate keyboard input, and can also be used to enter keys on the keyboard, even key combinations, such as the following example:
# Simulate keyboard events
From selenium import Webdriver
# Introducing the Keys module
From Selenium.webdriver.common.keys import keys
Driver = Webdriver.chrome ("Installation Tool \python\chromedriver.exe")
Driver.get ("http://www.baidu.com")
# Input Box input content
driver.find_element_by_id ("kw"). Send_keys ("Selenium")
# Enter "Tutorials"
driver.find_element_by_id ("kw"). Send_keys ("tutorial")
# Remove "Tutorials"
driver.find_element_by_id ("kw"). Send_keys (Keys.back_space)
...
The following are common keyboard operations:
# Common Keyboard Operations
Send_keys (keys.back_space) Delete key
Send_keys (keys.space) space key
Send_keys (Keys.tab) line break key
Send_keys (keys.escape) fallback key
Send_keys (keys.enter) Enter
Send_keys (Keys.control, ' a ') Select all
Send_keys (Keys.control, ' C ') copy
Send_keys (Keys.control, ' V ') paste
Send_keys (Keys.control, ' x ') cut
Send_keys (KEYS.F1) F1 key
...
Send_keys (KEYS.F12) F12 key
The above for Webdriver Control browser operation and simulation of keyboard, mouse operation of the common methods, of course, the actual use of the process, but also need a practical business scenario, flexible use!
(iv) Webdriver API: Control browser and mouse, keyboard events