selenium调用键盘

Posted chenfei2928

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了selenium调用键盘相关的知识,希望对你有一定的参考价值。

键盘

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

driver = webdriver.Chrome(executable_path=‘D:develop_studychromedriverchromedriver‘) # 打开浏览器
driver.maximize_window()    #浏览器最大化
driver.get(‘https://www.yuanrenxue.com‘) # 打开目标网站

#定位右上角搜索图标并点击
try:
    element = WebDriverWait(driver,10).until(
        EC.presence_of_element_located((By.CLASS_NAME,‘search-show‘))
    )
    element.click()
except:
    print(‘not locate serarch-show‘)
    driver.quit()



driver.find_element_by_class_name(‘search-show‘).click()
# 找到输入框
search = driver.find_element_by_class_name("search-input")
# 输入 Python教程
search.send_keys(u‘python教程‘)
search.send_keys(Keys.ENTER)


#time.sleep(30)

element = driver.find_element_by_tag_name(‘body‘)
time.sleep(10)
element.send_keys(Keys.DOWN)
time.sleep(10)
element.send_keys(Keys.DOWN)
time.sleep(5)
driver.quit()

键盘组合键

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time


driver = webdriver.Chrome(executable_path=‘D:develop_studychromedriverchromedriver‘)
driver.maximize_window()
driver.get(‘https://www.yuanrenxue.com‘)
time.sleep(5)
element = driver.find_element_by_tag_name(‘body‘)
element.send_keys(Keys.CONTROL,‘a‘)

element.send_keys(Keys.CONTROL,‘w‘)  #关闭窗口无效。

以上是关于selenium调用键盘的主要内容,如果未能解决你的问题,请参考以下文章

selenium+python - webdriver​模拟键盘ENTER没有效果

Python+Selenium笔记(十四)鼠标与键盘事件

selenium 3.0 键盘事件 +强制结束chromedriver进程代码

selenium键盘操作,执行JS,上传文件

selenium学习:键盘事件

Selenium:用By定位元素鼠标事件键盘事件