1. webdriver中常用属性
import time
from selenium import webdriver
driver = webdriver.Firefox()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www.baidu.com")
# 判断元素是否可见,返回结果为布尔值
res = driver.find_element_by_id("kw").is_displayed()
print(res)
# 判断元素是否选中,返回结果为布尔值
res = driver.find_element_by_id("kw").is_selected()
print(res)
# 判断元素是否可编辑,返回结果为布尔值
res = driver.find_element_by_id("kw").is_enabled()
print(res)
# 在输入框中输入消息
driver.find_element_by_id("kw").send_keys("selenium")
time.sleep(2)
# 提交表单(可看作模拟回车键)
driver.find_element_by_id("kw").submit()
time.sleep(2)
# 清空输入框中输入的内容
driver.find_element_by_id("kw").clear()
time.sleep(2)
# 得到元素的属性值
res = driver.find_element_by_id("kw").get_attribute('id')
print(res)
# 得到元素的大小
res = driver.find_element_by_id("su").size
print(res)
# 得到元素的文本
res = driver.find_element_by_link_text("贴吧").text
print(res)
# 点击元素
driver.find_element_by_link_text("贴吧").click()
time.sleep(2)
driver.quit()
2. 浏览器页面操作
# -*- coding:utf-8 -*-
# Author: Aaron
import time
from selenium import webdriver
driver = webdriver.Firefox()
# 最大化浏览器窗口
driver.maximize_window()
# 隐式等待
driver.implicitly_wait(30)
# 加载测试地址
driver.get("https://www.baidu.com/")
# 获取当前使用的浏览器
res = driver.name
print(res)
# 获取当前页面的标题
res = driver.title
print(res)
# 获取当前页面的地址
res = driver.current_url
print(res)
# 获取当前页面的代码
time.sleep(2)
res = driver.page_source
print(res)
driver.get("https://cn.bing.com/")
# 后退回上一个界面
time.sleep(2)
driver.back()
# 前进到下一个页面
time.sleep(2)
driver.forward()
# 刷新当前页面
time.sleep(2)
driver.refresh()
time.sleep(2)
'''
多窗口
'''
driver.get("http://mail.sina.com.cn/")
# 获取当前页面的句柄
time.sleep(3)
now_handle = driver.current_window_handle
print(now_handle)
driver.find_element_by_link_text('注册').click()
time.sleep(4)
# 获取所有句柄
handles = driver.window_handles
print(handles)
for handle in handles:
if handle != now_handle:
driver.switch_to.window(handle)
time.sleep(2)
driver.find_element_by_name('email').send_keys('Aaron')
time.sleep(2)
# 关闭注册页面
# driver.close()
# 切换为登录页面
driver.switch_to.window(now_handle)
driver.set_window_size(1080, 920)
time.sleep(2)
driver.find_element_by_id('freename').send_keys('Aaron')
time.sleep(2)
# 关闭当前页面
driver.close()
time.sleep(2)
# 退出驱动,并关闭所有页面
driver.quit()
3. 鼠标操作
'''
键盘操作步骤:
(1)导入ActionChains类
(2)定位需要操作的元素
(3)实例化对象
(4)调用ActionChains类的方法
'''
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www.baidu.com/")
# 鼠标悬停在"设置"上
element1 = driver.find_element_by_link_text("设置")
ActionChains(driver).move_to_element(element1).perform()
time.sleep(2)
# 双击左键文字"有事搜一搜"
element2 = driver.find_element_by_css_selector("div > p[class='sub-title']")
ActionChains(driver).double_click(element2).perform()
time.sleep(2)
# 单击右键
element3 = driver.find_element_by_id("su")
ActionChains(driver).context_click(element3).perform()
time.sleep(5)
driver.quit()
4. 键盘操作
'''
键盘操作步骤:
(1)导入Keys类
(2)在send_keys()中使用Keys类的属性
'''
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('https://www.baidu.com/')
element = driver.find_element_by_id("kw")
element.send_keys("selenium")
# 模拟全选操作
element.send_keys(Keys.CONTROL, 'a')
time.sleep(2)
# 模拟复制操作
element.send_keys(Keys.CONTROL, 'c')
time.sleep(2)
# 模拟删除键
element.send_keys(Keys.BACKSPACE)
time.sleep(2)
element.clear()
# 模拟粘贴键
element.send_keys(Keys.CONTROL, 'v')
time.sleep(2)
driver.quit()
5. 下拉框操作
'''
下拉框操作步骤:
(1)导入Select类
(2)定位select标签的元素
(3)实例化对象
(4)调用Select类的方法
'''
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
driver = webdriver.Firefox()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www.baidu.com/")
# 鼠标悬停在"设置"上
element1 = driver.find_element_by_link_text("设置")
ActionChains(driver).move_to_element(element1).perform()
time.sleep(2)
# 点击"高级搜索",进入高级搜索界面
driver.find_element_by_link_text("高级搜索").click()
# 定位"全部时间"下拉框的select标签
element = driver.find_element_by_css_selector('select[name="gpc"]')
# 实例化对象
slt = Select(element)
# 通过索引方式定位下拉选项
slt.select_by_index(1)
time.sleep(2)
slt.select_by_index(4)
time.sleep(2)
# 通过value属性定位
slt.select_by_value('stf')
time.sleep(2)
# 通过文本属性定位
slt.select_by_visible_text("最近一天")
time.sleep(2)
slt.select_by_visible_text("最近一月")
time.sleep(2)
driver.quit()
6. 弹出框操作
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.alert import Alert
driver = webdriver.Firefox()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www.baidu.com/")
ele = driver.find_element_by_link_text("设置")
ActionChains(driver).move_to_element(ele).perform()
time.sleep(2)
driver.find_element_by_link_text("搜索设置").click()
time.sleep(2)
driver.find_element_by_css_selector('.prefpanelgo').click()
time.sleep(2)
# 输出弹出框内容
res = Alert(driver).text
print("弹框提示内容为:\n" + res)
# 接受弹出框
Alert(driver).accept()
'''
另一种方法:
res = driver.switch_to_alert().text
print("弹框提示内容为:\n" + res)
driver.switch_to_alert().accept()
'''
'''
对于弹出框的处理还有两种:
1.弹出框有两个按钮:“确认”和“取消”,这时用accept()和dismiss()来处理;
2.弹出框包含一个文本输入框时:用send_keys()来处理。
'''
time.sleep(5)
driver.quit()
7. 窗口滚动条操作
'''
使用JavaScript脚本来辅助完成
用driver.execute_script(script)来实现
括号中的script为JavaScript脚本
'''
import time
from selenium import webdriver
driver = webdriver.Firefox()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('https://www.baidu.com/')
driver.find_element_by_css_selector('[id="kw"]').send_keys("selenium")
time.sleep(2)
driver.find_element_by_css_selector('[id="su"]').click()
time.sleep(2)
# 垂直滚动条
# 滑动滚动条到窗口底部
bottom = 'window.scrollTo(0, 10000)'
driver.execute_script(bottom)
time.sleep(2)
# 滑动滚动条到窗口顶部
top = 'window.scrollTo(0, 0)'
driver.execute_script(top)
time.sleep(2)
# 设置窗口大小为500*500
driver.set_window_size(500, 500)
time.sleep(3)
# 水平滚动条
# 滑动滚动条到窗口最右部
right = 'window.scrollTo(10000, 0)'
driver.execute_script(right)
time.sleep(2)
# 滑动滚动条到窗口最左部
right = 'window.scrollTo(0, 0)'
driver.execute_script(right)
time.sleep(2)
driver.quit()
8. 窗口截图操作
import time
from selenium import webdriver
driver = webdriver.Firefox()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('https://www.baidu.com/')
time.sleep(2)
# 截取浏览器窗口当前的界面保存为baidu_page1.png图片存放在路径E:\python下
driver.get_screenshot_as_file(r"E:\python\baidu_page1.png")
time.sleep(2)
# 等价于get_screenshot_as_file()
driver.save_screenshot(r"E:\python\baidu_page2.png")
driver.quit()