from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.keys import Keys
class TestActionChains():
def setup(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(5)
self.driver.maximize_window()
def teardown(self):
self.driver.quit()
def test_click(self):
self.driver.get('http://sahitest.com/demo/clicks.htm')
element_click=self.driver.find_element(By.XPATH,'//*[@value="click me"]')
element_doubleclick=self.driver.find_element(By.XPATH,'//*[@value="dbl click me"]')
element_rightclick=self.driver.find_element(By.XPATH,'//*[@value="right click me"]')
action=ActionChains(self.driver)
#点击
action.click(element_click)
#双击
action.double_click(element_doubleclick)
#右键
action.context_click(element_rightclick)
time.sleep(3)
action.perform()
time.sleep(3)
def test_move(self):
self.driver.get('https://ceshiren.com/c/40-category/40')
ele=self.driver.find_element(By.LINK_TEXT,'精华帖')
action=ActionChains(self.driver)
#鼠标移动到某个元素上
action.move_to_element(ele)
action.perform()
time.sleep(3)
def test_dragdrop(self):
self.driver.get('https://sahitest.com/demo/dragDropMooTools.htm')
ele_drag=self.driver.find_element(By.ID,'dragger')
ele_drop=self.driver.find_element(By.CLASS_NAME,'item')
action=ActionChains(self.driver)
#将一个元素拖拽到某一个元素上
action.drag_and_drop(ele_drag,ele_drop)
action.perform()
time.sleep(3)
def test_keys(self):
self.driver.get('http://sahitest.com/demo/label.htm')
ele_keys=self.driver.find_element(By.XPATH,'//*[@type="textbox"]').click()
action=ActionChains(self.driver)
#输入
action.send_keys('username').pause(1) #每步操作等待一秒
#空格
action.send_keys(Keys.SPACE).pause(1)
#在输入
action.send_keys('tom').pause(1)
#删除一个字母
action.send_keys(Keys.BACK_SPACE).pause(1)
action.perform()
time.sleep(3)
if __name__ == '__main__':
TestActionChains()