selenium 鼠标,键盘操作

1.鼠标操作

导包:from selenium.webdriver.common.action_chains import ActionChains

1.context_click()           右击

2.double_click()    双击

3.drag_and_drop()  拖动

4.move_to_element()   悬停

5.perform()      执行以上所有鼠标方法

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

driver=webdriver.Chrome()
action=ActionChains(driver)
driver.get('http://127.0.0.1/mgr/sign.html')
driver.implicitly_wait(5)
time.sleep(1)
user=driver.find_element_by_id('username')
user.send_keys('byhy')

#右击
action.context_click(user).perform()
#双击
action.double_click(user).perform()

#拖动 将div1拖到2上
source=driver.find_element_by_id('div1')
target=driver.find_element_by_id('div2')
time.sleep(1)
action.drag_and_drop(source,target).perform()

time.sleep(2)
driver.quit()

 

冻结画面,然后定位元素 

setTimeout(function(){debugger}, 5000)

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

driver=webdriver.Chrome()
action=ActionChains(driver)
driver.implicitly_wait(5)
driver.get('https://www.baidu.com/')
time.sleep(1)
more=driver.find_element_by_xpath('//*[@id="s-top-left"]/div/a')
action.move_to_element(more).perform()
driver.find_element_by_xpath('//*[@id="s-top-more"]/div[1]/a[4]/img').click()

time.sleep(2)
driver.quit()

 

2.键盘操作

导包:  from selenium.webdriver.common.keys import Keys

常用的键盘操作:

1.send_keys(Keys.BACK_SPACE)        删除键

2.send_keys(Keys.SPACE)            空格键

3.send_keys(Keys.TAB)                 制表键

4.send_keys(Keys.ESCAPE)            回退键

5.send_keys(Keys.ENTER)               回车键

6.send_keys(Keys.CONTROL,'a')          全选键

7.send_keys(Keys.CONTROL,'c')          复制键

 

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys

driver=webdriver.Chrome()
driver.implicitly_wait(5)
driver.get('https://www.baidu.com/')
time.sleep(1)
more=driver.find_element_by_id('kw')
more.send_keys('12345')
#全选
more.send_keys(Keys.CONTROL,'a')
#复制
more.send_keys(Keys.CONTROL,'c')
time.sleep(1)
#删除所有
more.send_keys(Keys.BACK_SPACE)
time.sleep(1)
#粘贴
more.send_keys(Keys.CONTROL,'v')


time.sleep(2)
driver.quit()

 

posted @ 2020-08-17 21:25  程程111  阅读(140)  评论(0编辑  收藏  举报