selenium 滑鼠,鍵盤操作
阿新 • • 發佈:2020-08-17
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()