一、简单操作
- click():点击
- send_keys():输入
- clear():清空
- submit():提交表单
- size:返回元素的尺寸
- text:获取元素的文本
- get_attribute():获取元素的属性值
- title:获取当前页面标题
- current_url:获取当前页面URL
- is_displayed():返回元素是否可见
输入、清空、点击操作
代码:
# coding = utf-8 from time import sleep from selenium import webdriver # 驱动文件路径 driverfile_path = r‘D:\coship\Test_Framework\drivers\chromedriver.exe‘ # 启动浏览器 driver = webdriver.Chrome(executable_path=driverfile_path) # 打开百度首页 driver.get(r‘https://www.baidu.com/‘) driver.find_element_by_css_selector("#kw").send_keys("selenium") sleep(2) driver.find_element_by_css_selector("#kw").clear() sleep(2) driver.find_element_by_css_selector("#kw").send_keys("selenium") driver.find_element_by_css_selector("#su").click() # 等待2秒退出浏览器进程 sleep(2) driver.quit()
submit()提交表单操作
一般用于模拟回车键(Enter)
代码:
# coding = utf-8 from time import sleep from selenium import webdriver # 驱动文件路径 driverfile_path = r‘D:\coship\Test_Framework\drivers\chromedriver.exe‘ # 启动浏览器 driver = webdriver.Chrome(executable_path=driverfile_path) # 打开百度首页 driver.get(r‘https://www.baidu.com/‘) driver.find_element_by_css_selector("#kw").send_keys("selenium") # 模拟回车键 driver.find_element_by_css_selector("#kw").submit() # 等待3秒退出浏览器进程 sleep(3) driver.quit()
获取元素信息操作
代码:
# coding = utf-8 from time import sleep from selenium import webdriver # 驱动文件路径 driverfile_path = r‘D:\coship\Test_Framework\drivers\chromedriver.exe‘ # 启动浏览器 driver = webdriver.Chrome(executable_path=driverfile_path) # 打开百度首页 driver.get(r‘https://www.baidu.com/‘) # 获取搜索输入框尺寸 size = driver.find_element_by_css_selector("#kw").size print("size=",size) # 获取页面底部备案信息 text = driver.find_element_by_css_selector("#cp").text print("text=",text) # 获取元素的属性值,如id,name,class,type以及其他属性 attribute = driver.find_element_by_css_selector("#kw").get_attribute("class") print("attribute=",attribute) # 检查元素是否可见,可见返回True,不可见返回False result = driver.find_element_by_css_selector("#kw").is_displayed() print("resulr=",result) # 获取当前页面标题 title = driver.title print("title=",title) # 获取当前页面URL url = driver.current_url print("url=",url) # 等待2秒退出浏览器进程 sleep(2) driver.quit()
二、键盘操作
webdriver提供了键盘上几乎所有的按键方法,使用前需要导入Keys类
from selenium.webdriver.common.keys import Keys
代码:
# coding = utf-8 from time import sleep from selenium import webdriver from selenium.webdriver.common.keys import Keys # 驱动文件路径 driverfile_path = r‘D:\coship\Test_Framework\drivers\chromedriver.exe‘ # 启动浏览器 driver = webdriver.Chrome(executable_path=driverfile_path) # 打开百度首页 driver.get(r‘https://www.baidu.com/‘) # 输入内容 driver.find_element_by_css_selector("#kw").send_keys("seleniumm") # 删除一个字符 driver.find_element_by_css_selector("#kw").send_keys(Keys.BACK_SPACE) # 输入空格键+"教程" driver.find_element_by_css_selector("#kw").send_keys(Keys.SPACE) driver.find_element_by_css_selector("#kw").send_keys("教程") # 全选输入框内容 driver.find_element_by_css_selector("#kw").send_keys(Keys.CONTROL,‘a‘) # 剪切内容 driver.find_element_by_css_selector("#kw").send_keys(Keys.CONTROL,‘x‘) # 粘贴内容 driver.find_element_by_css_selector("#kw").send_keys(Keys.CONTROL,"v") # 回车键 driver.find_element_by_css_selector("#kw").send_keys(Keys.ENTER) # 等待3秒退出浏览器进程 sleep(3) driver.quit()
三、鼠标操作
同样,webdriver也提供了一整套鼠标操作方式,如:右击,双击,悬停,拖动等,使用前需要导入ActionChains类。
from selenium.webdriver.common.action_chains import ActionChains
ActionChains类提供了鼠标操作的常用方法:
- perform():执行所有ActionChains中存储的行为
- context_click():右击
- double_click():双击
- drag_and_drop:拖动
- move_to_element():鼠标悬停
代码:
# coding = utf-8 from time import sleep from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains # 驱动文件路径 driverfile_path = r‘D:\coship\Test_Framework\drivers\chromedriver.exe‘ # 启动浏览器 driver = webdriver.Chrome(executable_path=driverfile_path) # 打开百度首页 driver.get(r‘https://www.baidu.com/‘) # 鼠标悬停在百度首页设置上 set = driver.find_element_by_link_text("设置") ActionChains(driver).move_to_element(set).perform() # 等待3秒退出浏览器进程 sleep(3) driver.quit()
原文地址:https://www.cnblogs.com/eastonliu/p/9101866.html
时间: 2024-11-13 08:01:17