3、常见的键盘操作键盘操作对应代码键盘F1到F12send_keys(Keys.F1) 把F1改成对应的快捷键复制Ctrl+Csend_keys(Keys.CONTROL,'c')粘贴Ctrl+Vsend_keys(Keys.CONTROL,'v')全选Ctrl+Asend_keys(Keys.CONTROL,'a')剪切Ctrl+Xsend_keys(Keys.CONTROL,'x')制表键Tabsend_keys(Keys.TAB)五、演示案例源码示例代码:
# -*- coding: utf-8 -*-"""@Time : 2022/10/25 21:39@Auth : 软件测试君@File :element_actions.py@IDE :PyCharm@Motto:ABC(Always Be Coding)"""import timefrom selenium.webdriver import Keys, ActionChainsfrom selenium.webdriver.common.by import Byfrom selenium import webdriverfrom selenium.webdriver.chrome.service import Servicefrom webdriver_manager.chrome import ChromeDriverManager'''初始化操作'''driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))def init(): # 最大化操作 driver.maximize_window() driver.set_script_timeout(60) # 智能等待找到元素后立即继续执行 , 全局生效 driver.implicitly_wait(60) driver.set_page_load_timeout(60)init()'''元素点击操作'''def clickDemo(): # 点击(鼠标左键)页面按钮:click() driver.get("http://localhost:8080/click.html") button1 = driver.find_element(By.ID, "button1") is_displayed = button1.is_enabled() if is_displayed: button1.click() # 关闭弹窗 driver.switch_to.alert.accept()### 元素基本操作clickDemo()time.sleep(1)'''submit操作'''def submitDemo(): # 点击(鼠标左键)页面按钮:submit() driver.get("http://localhost:8080/submit.html") login = driver.find_element(By.ID, "login") is_displayed = login.is_enabled() if is_displayed: login.submit() # login.click() # 小贴士:支持submit的肯定支持click,但是支持click的 , 不一定支持submit , 可能会报错如下:submitDemo()'''输入、清空输入操作'''def clearInputDemo(): # 输入、清空输入操作:clear() send_keys() username = driver.find_element(By.CSS_SELECTOR, "input[type='text']") username.clear() username.send_keys(u"公众号:软件测试君") # 输出:公众号:软件测试君 print('输入值:{0}'.format(username.get_attribute("value"))) time.sleep(1)clearInputDemo()'''模拟打开百度搜索输入博客园 , 回车操作'''def mockEnterDemo(): # 模拟打开百度搜索输入博客园 , 回车操作 示例代码 driver.get("https://www.baidu.com/") driver.find_element(By.ID, "kw").send_keys("久曲健 博客园", Keys.ENTER)### 键盘操作mockEnterDemo()def mouseDemo(): driver.get("http://localhost:8080/mouse.html") # 鼠标左键点击 ActionChains(driver).click(driver.find_element(By.ID, "mouse2")).perform() time.sleep(1) driver.switch_to.alert.accept() time.sleep(1) # 鼠标悬浮并移动操作 ActionChains(driver).move_to_element(driver.find_element(By.ID, "mouse1")).pause(1).move_to_element( driver.find_element(By.ID, "mouse6")).perform() time.sleep(1) driver.switch_to.alert.accept() # 鼠标双击操作 ActionChains(driver).double_click(driver.find_element(By.ID, "mouse3")).perform() time.sleep(1) driver.switch_to.alert.accept() # 鼠标右键 ActionChains(driver).context_click(driver.find_element(By.ID, "mouse5")).perform()### 常见键盘事件操作mouseDemo()time.sleep(3)driver.quit()
经验总结扩展阅读
- 2023年农历八月十四宜下葬吗 2023年9月28日下葬好吗
- 2023年农历八月十四宜入土吗 2023年9月28日入土行吗
- 2023年农历八月十四宜穿丧服吗 2023年农历八月十四穿丧服吉日
- 2023年农历八月十四宜立墓碑吗 2023年9月28日立墓碑黄道吉日
- 2023年9月28日适合开造陵墓吗 2023年农历八月十四开造陵墓吉日
- 2023年9月28日入棺好吗 2023年农历八月十四宜入棺吗
- 2023年农历八月十四宜进棺材吗 2023年9月28日进棺材吉日一览表
- 2023年农历八月十四宜粉刷墙壁吗 2023年农历八月十四粉刷墙壁吉日
- 2023年农历八月十四宜修理仓库吗 2023年9月28日修理仓库好吗
- 2023年9月28日建造仓库吉日一览表 2023年农历八月十四建造仓库吉日