鍵盤操作:全選、復(fù)制棍郎、剪切、粘貼
實(shí)現(xiàn)方式:
需要引入Keys()
from selenium.webdriver.common.keys import Keys定位元素并執(zhí)行鍵盤相關(guān)操作
案例: 在百度搜索關(guān)鍵詞“Python” 然后將關(guān)鍵詞復(fù)制或剪切到搜狗搜索框進(jìn)行搜索
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
driver = webdriver.Chrome()
# 打開百度瀏覽器
driver.get("https://www.baidu.com/")
# 搜索框輸入python
driver.find_element_by_id('kw').send_keys('python')
sleep(2)
# 全選搜索框內(nèi)容
driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'a')
# 復(fù)制
driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'c')
# 剪切
driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'x')
# 打開搜狗瀏覽器并粘貼到搜索框
driver.get("https://www.sogou.com/")
driver.find_element_by_css_selector('.sec-input').send_keys(Keys.CONTROL, 'V')
# 進(jìn)行搜索
driver.find_element_by_css_selector('#stb').click()
sleep(2)
driver.quit()