selenium的使用(四)

今天写一下关于模拟按键

用到的: ActionChains

导入:

from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver

  url = 'xxxxxxxxxxxxx'

browser = webdriver.Chrome()

browser.get(url)

模拟鼠标

注意:   .perform()    使操作执行

左键单击: 

a = browser.find_element_by_xpath()         定位元素

ActionChains(browser).click(a).perform()                可以实现单击鼠标左键

 

长按左键:

aa  = browser.find_element_by_xpath()

ActionChains(browser).click_and_hold(aa).perform()

 

双击: 

b = browser.find_element_by_xpath()

ActionChains(browser).double_click(b).perform()

 

右键单击: 

c = browser.find_elemnt_by_xpath()

ActionChains(browser).context_click(c).perform()

 

松开鼠标:  .release()

ActionChains(browser).cilck_and_hold(a).release(a).perform()

 

  拖拽:

#起始元素:

start = browser.find_element_by_xpath()

#目标元素:

end = browser.find_element_by_xpath()

#进行拖拽:

ActionChains(browser).drag_and_drop(start,end).perform()

 

移动鼠标到元素位置:

.move_to_element()

d = browser.find_element_by_xpath()

ActionChains(browser).move_to_element(d).perform()

在运行代码时,桌面上的鼠标没动。但是确实移动了鼠标,只不过我们看不见。

 

移动鼠标到某个坐标:

.move_by_offset(xoffset,yoffset)

#两个形参分别是横坐标和纵坐标。

模拟键盘

导入:

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.action_chains import ActionChains

 

  按下键盘按键:

  .key_down()

  松开键盘按键:

.key_up()

利用上面两个可以实现一些组合按键,如:ctrl+c,ctrl+v

实例:Ctrl + C

ActionChains(browser).key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()

 

posted @ 2018-03-23 16:40  嚯嚯嚯  阅读(162)  评论(0)    收藏  举报