爬虫之selenium和webdriver—基础(三):行为链

有时候在页面的操作很多,那么这时候可以使用行为链类ActionChains类完成。比如 现在要将鼠标移动到某个元素上并执行点击事件。

 1 from selenium import webdriver
 2 from selenium.webdriver.common.action_chains import ActionChains
 3 driver_path = 'D:\chromedriver\chromedriver.exe'
 4 driver = webdriver.Chrome(executable_path=driver_path)
 5 driver.get('https://www.baidu.com')
 6 
 7 
 8
 9 inputTag = driver.find_element_by_id('kw') #输入框
10 submitTag = driver.find_element_by_id('su') #button按钮
11 actions = ActionChains(driver)
12 actions.move_to_element(inputTag) #把鼠标移动到输入框上
13 actions.send_keys_to_element(inputTag, 'python') #在输入框输入python
14 actions.move_to_element(submitTag) #将鼠标移动到button按钮上
15 actions.click(submitTag) #点击
16 actions.perform() #将上面所有的行为串联起来

还可以写在一起:ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()

 

一些常见的的鼠标操作:

click_and_hold(element):点击但不松开鼠标

context_click(element):右键点击

double_click(element):双击

posted @ 2020-06-11 15:03  牛公的跑奔  阅读(340)  评论(0编辑  收藏  举报
总访问量:AmazingCounters.com