Selenium - Webdriver API /ActionChains API

1. Control the browser

1.1 Control the size of the browser window

# Get the size of the current browser driver . get_window_size (

# Set the size of the browser in pixels driver .set_window_size ( ' width' , 'height' )

# Get the x,y coordinates of the current window's position relative to Windows   driver . get_window_position ( )

# Set the position of the current window for Windows, x,y driver . set_window_position ( 20 , 20 )

# Maximize the current window, no need to pass parameter   driver . maximize_window ( )

# Returns the browser handle for the current operation driver .current_window_handle

# Returns all browser handles that open the server driver . window_handles
1.2 Control browser back and forward
# Forward driver.forward ( )
# back driver.back ( )
1.3 Simulate browser refresh
# refresh   driver . refresh ( )
2. Simple element operation
2.1 Common methods
#Clear the content of the text input box element . clear() 
#Simulate the keyboard to input content into the input box   element . send_keys(*value)
#Used to click an element   element . click()   is provided that it is an object that can be clicked. It and the send_keys(*value) method are the two most commonly used methods in Web page operations
The click() method can not only be used to click a button, it can also click any clickable text, image link, checkbox, radio button, drop-down box
2.2 Common methods of WebElement interface
Often the interesting and required methods of interacting with the page are provided by the WebElement interface
#Imitate the Enter button to submit data element . submit (
# According to the label attribute name, get the attribute value   element.get_attribute('style')
# Get property   element.get_property('id')
# Returns whether the element is visible True or False   element.is_displayed()
# Returns whether the element is selected True or False   element.is_selected()
# Returns the name of the tag element   element.tag_name
# Get the width and height of the current label   element . size
# Get the text content of the   element element . text
# Get the coordinates of the current element   element.location
# 截取图片  element.screenshot()
2.3  其他
执行JavaScript语句  driver.execute_script('JavaScript Commond')
例如 通过js来操作滚动条 driver.execute_script('window.scrollTo(0,0);')
三、 鼠标操作
在Webdriver 中,鼠标操作的方法封装在ActionChains 类中
引入方式: from selenium.webdriver.common.action_chains import ActionChains
3.1 鼠标右击操作
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
# 导入提供鼠标操作的ActionChains
driver = webdriver.Chrome()
driver.get('http://ui.imdsx.cn/uitester/')
driver.maximize_window() #最大化当前窗口
driver.execute_script('window.scrollTo(0,0);')
#执行JavaScript语句,通过js 来操作滚动条
right_click = driver.find_element_by_id('id1')
ActionChains(driver).context_click(right_click).perform()

ActionChains(driver)
调用ActionChains() 类,将浏览器驱动driver作为参数传入
context_click(right_click)
context_click( )方法用于模拟鼠标右键操作,在调用时需要指定元素定位
perform()
执行所有ActionChains 中存储的行为,可以理解成是对整个操作的提交动作
3.2 鼠标悬停 move_to_element()
move_to_element() 方法可以模拟鼠标悬停的动作,其用法与context_click( )相同
dive = driver.find_element_by_css_selector('#a')
ActionChains(driver).move_to_element(dive).perform()
3.3 鼠标双击操作 double_click()

3.4 鼠标拖放操作 drag_and_drop(source,target)
source 鼠标拖动的源元素
target 鼠标释放的目标元素
s = driver.find_element_by_css_selector('#dragger')
t = driver.find_element_by_css_selector('#i1')
ActionChains(driver).drag_and_drop(s,t).perform()
 
引用方法 :from selenium.webdriver.common.keys import Keys
# key_down 模拟键盘摁下某个按键 key_up 松开某个按键,与sendkey连用完成一些操作,每次down必须up一次否则将出现异常
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()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324894926&siteId=291194637