一、定位元素
1. id定位
find_element_by_id("kw")
2. name定位
find_element_by_name("wd")
3. class定位
find_element_by_class_name("s_ipt")
4. tag定位
find_element_by_tag_name("input")
5. link定位
driver.find_element_by_link_text("直播")
6. partial link定位
find_element_by_partial_link_text("直")
7. XPath定位
7.1 绝对路径定位(每次加载时可能会变)
find_element_by_xpath("/html/body/div/div[2]/div[2]/a[4]")
7.2 利用元素属性定位
# 表示当前页面的标签id属性为kw的element,*表示不指定标签
find_element_by_xpath("//*[@id='kw']")
7.3 层级与属性结合
find_element_by_xpath("//span[@class='bg s_ipt_wr new-pmd quickdelete-wrap']/input")
7.4 使用逻辑运算符
find_element_by_xpath("//input[@class='s_ipt' and @id='kw']")
7.5 使用contains方法(用于匹配一个属性中包含的字符串)
find_element_by_xpath("//span[contains(@class,'s_ipt_wr')]/input")
7.6 使用text()方法(用于匹配显示文本信息)
find_element_by_xpath("//a[text()='新闻']")
find_element_by_xpath("//a[contains(text(),'新')]")
8. CSS定位
8.1 通过class定位
find_element_by_css_selector(".s_ipt")
8.2 通过id定位
find_element_by_css_selector("#kw")
8.3 通过标签名定位
find_element_by_css_selector("input")
8.4 通过标签层级关系定位
# 查找span标签中所有标签名为input的子元素
find_element_by_css_selector("span > input")
8.5 通过属性定位
find_element_by_css_selector("[name='wd']")
8.6 组合定位
find_element_by_css_selector("form.fm > span > input.s_ipt")
8.7 更多定位用法
# 查找class属性包含s_ipt_wr字符串的元素
find_element_by_css_selector("[class*=s_ipt_wr]")
# 查找class属性以bg字符串开头的元素
find_element_by_css_selector("[class^=bg]")
# 查找class属性以wrap字符串结尾的元素
find_element_by_css_selector("[class$=wrap]")
# 查找form标签下第二个input标签元素
find_element_by_css_selector("form > input:nth-child(2)")
9. 使用By定位元素
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.find_element(By.ID, "kw")
driver.find_element(By.NAME, "wd")
driver.find_element(By.CLASS_NAME, "s_ipt")
driver.find_element(By.TAG_NAME, "input")
driver.find_element(By.LINK_TEXT, "新闻")
driver.find_element(By.PARTIAL_LINK_TEXT, "新")
driver.find_element(By.XPATH, "//*[@class='bg s_btn']")
driver.find_element(By.CSS_SELECTOR, "input#su")
二、控制浏览器
1. 控制浏览器窗口大小
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# 设置浏览器窗口大小,参数数字为像素
driver.set_window_size(480, 800)
2. 控制浏览器后退、前进
from selenium import webdriver
driver = webdriver.Chrome()
# 访问百度首页
first_url = "https://www.baidu.com"
print("now access %s" % first_url)
driver.get(first_url)
# 访问新网页
second_url = "https://www.hao123.com"
print("now access %s" % second_url)
driver.get(second_url)
# 回退
print("back to %s" % first_url)
driver.back()
# 前进
print("forward to %s" % second_url)
driver.forward()
driver.quit()
3. 模拟浏览器刷新
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# 刷新网页
driver.refresh()
driver.quit()
三、WebDriver中的常用方法
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# 清除文本
driver.find_element_by_id("kw").clear()
# 模拟按键输入
driver.find_element_by_id("kw").send_keys("iphone 13")
# 单击元素
driver.find_element_by_id("su").click()
driver.quit()
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
search_text = driver.find_element_by_id("kw")
search_text.send_keys("iphone 13")
# 提交表单,针对不提供搜索按钮通过键盘回车键完成提交
driver.find_element_by_id("su").submit()
driver.quit()
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# 返回元素的尺寸
size = driver.find_element_by_id("kw").size
print(size)
# 获取元素的文本
text = driver.find_element_by_class_name("s-top-left").text
print(text)
# 返回元素属性值
attribute = driver.find_element_by_id("kw").get_attribute('type')
print(attribute)
# 返回元素是否用户可见
result = driver.find_element_by_id("kw").is_displayed()
print(result)
driver.quit()
四、鼠标操作
from selenium import webdriver
# 引入ActionChains类,与鼠标相关的常用方法都封装在这个类中
from selenium.webdriver import ActionChains
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# 定位到要悬停的元素
above = driver.find_element_by_id("s-usersetting-top")
# 执行鼠标悬停操作,move_to_element()模拟鼠标移动到元素上,perform()提交在类中存储的行为
ActionChains(driver).move_to_element(above).perform()
# 右击
ActionChains(driver).context_click(above).perform()
# 双击
ActionChains(driver).double_click(above).perform()
driver.quit()
五、键盘操作
from selenium import webdriver
# 调用Keys模块
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# 在输入框输入内容
driver.find_element_by_id("kw").send_keys("iphone 131")
# 删除
driver.find_element_by_id("kw").send_keys(Keys.BACK_SPACE)
# 输入空格键
driver.find_element_by_id("kw").send_keys(Keys.SPACE)
driver.find_element_by_id("kw").send_keys("官网")
# 全选Ctrl + a
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'a')
# 剪切Ctrl + x
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'x')
# 粘贴
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'v')
# 回车
driver.find_element_by_id("kw").send_keys(Keys.ENTER)
driver.quit()
六、获得验证信息
from time import sleep
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
print("Before search...")
# 打印当前页面title
title = driver.title
print("title:" + title)
# 打印当前页面url
now_url = driver.current_url
print("URL: " + now_url)
driver.find_element_by_id("kw").send_keys("iphone")
driver.find_element_by_id("su").click()
sleep(2)
print("After search...")
# 打印当前页面title
title = driver.title
print("title: " + title)
# 打印当前页面url
now_url = driver.current_url
print("URL: " + now_url)
# 获取搜索结果条数(class属性中间有空格表示有多个属性)
num = driver.find_element_by_class_name("nums").text
print("result: " + num)
driver.quit()
七、设置元素等待
1. 显式等待
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# 显示等待是WebDriver等待某个条件成立则继续执行,否则在达到最大时长抛出超时异常
# WebDriverWait方法在设置时间内,默认每隔一段时间检测一次当前页面元素是否存在,如果超过设置时间则抛出异常,默认抛出NoSuchElementException
# until(method, message="")方法,调用该方法提供的驱动程序作为一个参数,指导返回值为true
# expected_conditions类提供预期条件判断方法
element = WebDriverWait(driver, 5, 0.5).until(EC.presence_of_element_located((By.ID, "kw")), "Error")
element.send_keys('selenium')
driver.quit()
2. 隐式等待
from time import ctime
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
driver = webdriver.Chrome()
# 设置隐式等待为10s,他会等待页面上的所有元素
driver.implicitly_wait(10)
driver.get("https://www.baidu.com")
try:
print(ctime())
driver.find_element_by_id("kw22").send_keys("iphone")
except NoSuchElementException as e:
print(e)
finally:
print(ctime())
driver.quit()
八、定位一组元素
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.find_element_by_id("kw").send_keys("iphone")
driver.find_element_by_id("su").click()
sleep(2)
# 定位一组元素
texts = driver.find_elements_by_xpath("//div[@tpl='se_com_default']/h3/a")
print(len(texts))
for t in texts:
print(t.text)
driver.quit()
九、多表单切换
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.get("https://mail.qq.com/")
sleep(2)
login_frame = driver.find_element_by_css_selector("iframe[id='login_frame']")
# switch_to.frame默认可以直接对表单的id属性或name属性传参
driver.switch_to.frame(login_frame)
driver.find_element_by_id('u').send_keys("1125868410@qq.com")
driver.find_element_by_id('p').send_keys("jzx20010907")
driver.find_element_by_id('login_button').click()
# 回到最外层页面
driver.switch_to.default_content()
driver.quit()
十、多窗口切换
import time
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("https://www.baidu.com")
sleep(2)
# 获得百度搜索窗口的句柄
search_windows = driver.current_window_handle
driver.find_element_by_link_text('登录').click()
driver.find_element_by_link_text('立即注册').click()
# 获得当前所有打开的窗口句柄
all_handles = driver.window_handles
# 进入注册窗口
for handle in all_handles:
if handle != search_windows:
# 切换到相应窗口
driver.switch_to.window(handle)
print(driver.title)
driver.find_element_by_name("userName").send_keys("username")
driver.find_element_by_name("phone").send_keys("16624709579")
time.sleep(2)
# 关闭当前窗口
driver.close()
# 回到搜索窗口
driver.switch_to.window(search_windows)
print(driver.title)
driver.quit()
十一、警告框处理
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# 打开搜索设置
link = driver.find_element_by_id("s-usersetting-top").click()
driver.find_element_by_link_text("搜索设置").click()
sleep(2)
# 保存设置
driver.find_element_by_class_name("prefpanelgo").click()
# 获取警告框
alert = driver.switch_to.alert
# 获取警告框中的文字信息
alert_text = alert.text
print(alert_text)
# 拉起警告框
alert.accept()
driver.quit()
十二、下拉框处理
# 用于定位<select>标签
from selenium.webdriver.support.select import Select
# 通过value值定位下拉选项
select_by_value()
# 通过text值定位下拉选项
select_by_visible_text()
# 根据下拉选项的索引进行选择
select_by_index()
十三、上传文件
1.普通上传:将本地文件路径作为一个值放在input标签中,通过form表单将这个值提交给服务器;
2.插件上传:基于Flash、Javascript或Ajax等技术实现的上传功能。
十四、下载文件
import os
from selenium import webdriver
options = webdriver.ChromeOptions()
# 'profile.default_content_setting.popups': 0表示禁止弹出下载窗口
# 'download.default_directory': os.getcwd()设置文件下载路径
prefs = {'profile.default_content_setting.popups': 0,
'download.default_directory': os.getcwd()}
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://pypi.org/project/selenium/#files")
driver.find_element_by_link_text("selenium-3.141.0.tar.gz").click()
十五、操作Cookie
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
# 获得所有的cookie信息并打印
cookie = driver.get_cookies()
print(cookie)
# 添加cookie信息
driver.add_cookie({'name': 'key-aaaaaaa', 'value': 'value-bbbbb'})
# 遍历指定的cookie
for cookie in driver.get_cookies():
print("%s->%s" % (cookie['name'], cookie['value']))
# 删除cookie
driver.delete_cookie("key-aaaaaaa")
for cookie in driver.get_cookies():
print("%s->%s" % (cookie['name'], cookie['value']))
# 删除所有cookie
driver.delete_all_cookies()
print(cookie)
十六、调用Javascript
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.set_window_size(800, 600)
driver.find_element_by_id("kw").send_keys("iphone")
driver.find_element_by_id("su").click()
# 通过JavaScript设置浏览器窗口的滚动条位置
js = "window.scrollTo(100, 450);"
driver.execute_script(js)
十七、处理HTML5视频播放
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.get("http://videojs.com/")
video = driver.find_element_by_id("preview-player_html5_api")
# 返回文件播放地址
url = driver.execute_script("return arguments[0].currentSrc;", video)
print(url)
# 播放视频
print("start")
driver.execute_script("arguments[0].play()", video)
# 播放15s
sleep(15)
# 暂停视频
print("stop")
driver.execute_script("arguments[0].pause()", video)
driver.quit()
十八、滑动解锁
十九、窗口截图
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://www.baidu.com')
driver.save_screenshot(".//baidu_img.png")
二十、关闭窗口
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://www.baidu.com')
# 退出相关驱动程序和关闭所有窗口,close()关闭当前窗口
driver.quit()