selenium--鼠标键盘事件

#####################################################
## 键盘&鼠标事件
#####################################################
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select

## 键盘事件
b = webdriver.Chrome()
b.implicitly_wait(1)
b.get('http://www.baidu.com')
b.maximize_window()

ipt_keyword = b.find_element_by_id('kw')

ipt_keyword.send_keys('abcdegf')

for i in range(6):
    ipt_keyword.send_keys(Keys.BACKSPACE)
    sleep(1)

# 最low的方法
# ipt_keyword.clear()
# ipt_keyword.send_keys('a')

sleep(3)
b.quit()



## 鼠标悬停

b = webdriver.Chrome()
b.implicitly_wait(1)
b.get('http://www.baidu.com')
b.maximize_window()

lnk_more = b.find_element_by_link_text('更多产品')

ActionChains(b).move_to_element(lnk_more).perform()

lnk_wenku = b.find_element_by_link_text("文库")
lnk_wenku.click()

sleep(3)
b.quit()



## 鼠标拖拽

b = webdriver.Chrome()
b.implicitly_wait(1)
b.get('file:///E:/ZM20190316/2.Selenium/TestDemo/drag&DropDemo/Drag&DropDemo.html')
b.maximize_window()

ele1 = b.find_element_by_id('dragger')

eles = b.find_elements_by_class_name("item")

for e in eles:
    ActionChains(b).drag_and_drop(ele1, e).perform()
    sleep(1)

# for i in range(len(eles)):
#     ActionChains(b).drag_and_drop(ele1, eles[i]).perform()
#     sleep(1)

sleep(3)
b.quit()

 

posted @ 2019-06-12 18:08  い昔阳ゾ  阅读(150)  评论(0编辑  收藏  举报