First, control the browser
1.1 Controlling the browser window size
# Gets the size of the current browser driver. Get_window_size()
# set the browser size driver by pixel. Set_window_size(' width ',' height ')
# Gets the coordinates x, y driver of the current window's location for Windows . Get_window_position()
# Set the current window's location for Windows, x, y driver. Set_window_position(a)
# Maximize the current window without the need to pass the driver. Maximize_window()
# returns the browser handle driver of the current operation. Current_window_handle
# Returns the browser handle driver for all open servers. Window_handles
1.2 Controlling the browser back, forward# Forward driver. Forward() # Back Driver. Back()
1.3 Simulating browser refreshes # Refresh driver. Refresh()
second, simple element operation
2.1 Common Methods#清除文本输入框中的内容 element. Clear ()#模拟键盘向输入框里输入内容element. Send_keys (*value) #用来单击一个元素element. The Click () assumes that it is an object that can be clicked, and that the Send_keys (*value) method is the two most commonly used method in Web page operationsThe Click () method can be used not only for clicking a button, but also for clicking any text, picture link, check box, Radio box, drop-down box that you can click
Common methods of 2.2 Webelement interfaceoften interesting and methods that need to interact with the page are provided by the Webelement interface#模仿回车按钮 Submit data element. Submit() # Gets the property value element according to the Tag property name . Get_attribute(' style ') < Span class= "CRAYON-E" > # Get Properties by property name element. Get_property ( ' id ' ) < Span class= "CRAYON-E" > # returns whether the element is visible True or False element. Is_displayed () # Returns whether the element is selected True or False . is_selected() # Returns the name element of the label elements . Tag_name # Gets the width and height element of the current label . Size # Gets the text content element of the elements . Text # Gets the current element's coordinate elements . Location # capture picture element. Screenshot()
2.3 Otherexecutes the JavaScript statement driver. Execute_script(' JavaScript commond ') For example , using JS to manipulate the scrollbar Driver.execute_script (' Window.scrollto (0,0); ')
third, mouse operationIn Webdriver, the method of mouse manipulation is encapsulated in the Actionchains classIntroduction Method: from selenium. Webdriver. Common. Action_chains Import actionchains
3.1 Mouse Right-click action
From Selenium import Webdriver
From selenium.webdriver.common.action_chains import actionchains
# import Actionchains to provide mouse operation
Driver = Webdriver. Chrome ()
driver.get (' http://ui.imdsx.cn/uitester/')
Driver.maximize_window () #最大化当前窗口
driver.execute_script (' Window.scrollto (0,0); ')
#执行JavaScript语句, using JS to manipulate the scroll bar
Right_click = driver.find_element_by_id (' id1 ')
actionchains (Driver). Context_click (Right_click). Perform ()
Actionchains (Driver)
Call the Actionchains () class to pass the browser driver driver as a parameter
Context_click (Right_click)
The Context_click () method is used to simulate the right mouse button operation and requires that the specified element be positioned when called
Perform ()
Perform all the stored behavior in the Actionchains, which can be interpreted as a commit action for the entire operation
3.2 Mouse Hover move_to_element ()
The Move_to_element () method simulates a mouse hover action with the same usage as Context_click ()
Dive = driver.find_element_by_css_selector (' #a ')
Actionchains (Driver). Move_to_element (Dive). Perform ()
Double_click()
Drag_and_drop (Source,target)
Source mouse Drag from element
Target mouse-released destination element
s = driver.find_element_by_css_selector (' #dragger ')
t = driver.find_element_by_css_selector (' #i1 ')
Actionchains (Driver). Drag_and_drop (S,t). Perform ()
Citation method: From Selenium.webdriver.common.keys import keys
# Key_down Analog Keyboard Press a key KEY_UP release a key, with Sendkey to complete some operations, each down must be up one time or an exception will occur
Actionchains (Driver). Key_down (Keys.control,dom). Send_keys (' a '). Send_keys (' C ').
Key_up (Keys.control). Key_down (KEYS.CONTROL,DOM1). Send_keys (' V '). Key_up (Keys.control). Perform ()
Selenium-webdriver api/actionchains API