1.Selenium环境安装
需要安装浏览器和浏览器驱动
采用谷歌浏览器为例。先下载谷歌浏览器,然后再下载官方驱动 Chrome for Testing availability
驱动版本号要和浏览器版本号对应符合(至少是大版本)
为了防止驱动更新后版本失效,可以选择关闭自动更新
2.创建浏览器、设置、打开
from selenium import webdriver # 用于操作浏览器 from selenium.webdriver.chrome.options import Options # 设置谷歌浏览器 from selenium.webdriver.chrome.service import Service # 用于管理谷歌驱动 # 创建设置浏览器对象 q1 = Options() # 禁用沙盒模式:增加系统兼容性,有的电脑不加此代码会闪退 q1.add_argument("--no-sandbox") # 保持浏览器打开状态(默认是代码执行完毕自动关闭) q1.add_experimental_option('detach', True) # 创建并启动浏览器 a1 = webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1)
3.打开网页、关闭网页、浏览器
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service import time # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() # 打开指定网址 a1.get('https://baidu.com') time.sleep(2) # 关闭当前标签页 a1.close() # 退出浏览器 a1.quit()
4.浏览器最大化、最小化
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service import time # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() # 打开指定网址 a1.get('https://baidu.com') time.sleep(2) # 浏览器最大化 a1.maximize_window() time.sleep(2) # 浏览器最小化 a1.minimize_window()
5.浏览器打开位置、尺寸
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() # 打开指定网址 a1.get('https://baidu.com') # 浏览器打开位置(x,y) a1.set_window_position(0, 0) # 浏览器打开尺寸(宽, 高) a1.set_window_size(500, 500)
6.浏览器截图、网页刷新
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() # 打开指定网址 a1.get('https://baidu.com') # 浏览器截图 a1.get_screenshot_as_file('1.png') time.sleep(3) # 刷新当前网页 a1.refresh()
7.元素定位
元素定位需要一点html相关知识,可以优先了解一下。
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # 引入新的包 # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() # 打开指定网址 a1.get('https://www.baidu.com/') # 定位一个元素(找到返回结果,找不到报错) a2 = a1.find_element(By.ID, 'kw') # 定位多个元素(找到的话返回列表,找不到的话返回空列表) a2 = a1.find_elements(By.ID, 'kw') print(a2) # 在浏览器中,可以采用 document.getElementById('元素值') 来查找元素
8.元素交互操作
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # 引入新的包 # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() a1.get('https://www.baidu.com/') a2 = a1.find_element(By.ID, 'kw') # 元素输入 a2.send_keys("selenium") time.sleep(2) # 元素清空 a2.clear() time.sleep(2) # 元素输入 a2.send_keys("selenium") # 获取搜索按钮 a2 = a1.find_element(By.ID, 'su') # 元素点击 a2.click()
9.八大定位
1.ID
根据标签的id进行定位
特点:通过ID定位元素,一般比较准确,但并不是所有的网页或者元素都有ID值
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # 引入新的包 # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() a1.get('https://www.baidu.com/') a1.find_element(By.ID, 'kw').send_keys("selenium")
2.NAME
根据标签的name进行定位
特点:通过NAME定位元素,一般比较准确,但并不是所有的网页或者元素都有NAME值
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # 引入新的包 # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() a1.get('https://www.baidu.com/') a1.find_element(By.NAME, 'wd').send_keys("selenium")
3.CLASS_NAME
根据标签的class进行定位
特点:class值不能有空格,否则会报错。class的重复值有很多,需要切片。class值有的网站是随机的
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # 引入新的包 # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() a1.get('https://www.bilibili.com/') a1.find_elements(By.CLASS_NAME, 'channel-link')[1].click()
4.TAG_NAME
根据标签名称定位,如a标签、div标签、p标签等
特点:重复的标签名字特别多,需要切片
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # 引入新的包 # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() a1.get('https://www.baidu.com/') a1.find_elements(By.TAG_NAME, 'a')[3].click()
5.LINK_TEXT
针对a标签,通过a标签的文本内容定位
特点:通过精准链接文本找到标签a的元素。可能有重复的文本
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # 引入新的包 # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() a1.get('https://www.baidu.com/') a1.find_element(By.LINK_TEXT, '新闻').click()
6.PARTIAL_LINK_TEXT
针对a标签,通过a标签的文本内容定位
特点:通过模糊链接文本找到标签a的元素。可能有重复的文本
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # 引入新的包 # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() a1.get('https://www.baidu.com/') a1.find_element(By.PARTIAL_LINK_TEXT, '新').click()
7.CSS_SELECTOR
使用方法:
1.#id = 井号 + id值 : 通过id定位
2..class = 点+class值,通过class定位
3.不加修饰符 = 标签头 通过标签头定位
4.通过任意类型定位:"[类型=‘精确值’]"
5.通过任意类型定位:"[类型*=‘模糊值’]"
6.通过任意类型定位:"[类型^=‘开头值’]"
7.通过任意类型定位:"[类型$=‘结尾值’]"
8.最实用方法:在谷歌控制台右键选中元素,复制selector
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # 引入新的包 # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() a1.get('https://www.baidu.com/') # 1.#id = 井号 + id值 : 通过id定位 # a1.find_element(By.CSS_SELECTOR, '#kw').send_keys('selenium') # 2..class = 点+class值,通过class定位 # a1.find_element(By.CSS_SELECTOR, '.s_ipt').send_keys('selenium') # 3.不加修饰符 = 标签头 通过标签头定位 # a1.find_elements(By.CSS_SELECTOR, 'input')[7].send_keys('selenium') # 4.通过任意类型定位:"[类型=‘精确值’]" # a1.find_element(By.CSS_SELECTOR, "[autocomplete='off']").send_keys('selenium') # 5.通过任意类型定位:"[类型*=‘模糊值’]" # a1.find_element(By.CSS_SELECTOR, "[autocomplete*='ff']").send_keys('selenium') # 6.通过任意类型定位:"[类型^=‘开头值’]" # a1.find_element(By.CSS_SELECTOR, "[autocomplete^='of']").send_keys('selenium') # 7.通过任意类型定位:"[类型$=‘结尾值’]" # a1.find_element(By.CSS_SELECTOR, "[autocomplete$='ff']").send_keys('selenium') # 8.最实用方法:在谷歌控制台右键选中元素,复制selector a1.find_element(By.CSS_SELECTOR, '#s-top-left > a:nth-child(3)').click()
8.XPATH
使用方法:
1.复制浏览器 Xpath(通过属性+路径定位)
2.复制浏览器 Xpath 完整路径
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # 引入新的包 # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() a1.get('https://www.baidu.com/') # 1.复制浏览器 Xpath(通过属性+路径定位) # a1.find_element(By.XPATH, '//*[@id="s-top-left"]/a[3]').click() # 2.复制浏览器 Xpath 完整路径 a1.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[3]/a[3]").click()
10.元素定位隐性等待
当访问一个网页时,若网页还未被渲染,可能会出现查找不到报错的情况,为了解决这种情况,需要添加一个延时等待。
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # 引入新的包 # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() a1.get('https://www.baidu.com/') # 元素定位隐性等待(多少秒内找到元素就立即执行,没有找到元素就报错) # 针对a1进行设置,a1在所有查找过程中,均会进行隐性等待,不需要重复设置 a1.implicitly_wait(30) a1.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[3]/a[3]").click()
11.单选、多选、下拉元素交互
单选框:点击想要选中的单选框
多选框:点击想要选中的多选框
下拉元素:点击想要选中的下拉元素,有时也需要先点击下拉框,再点击下拉元素
12.日期、评星、上传元素交互
日期元素:大部分日期输入本质就是输入框,可以直接输入
评星元素:大部分评星本质是按钮,可以直接点击
上传元素:大部分上传本质是一个输入框,输入需要上传文件的绝对路径即可
13.获取句柄、切换标签页
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # 引入新的包 # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() a1.get('https://www.baidu.com/') # 元素定位隐性等待(多少秒内找到元素就立即执行,没有找到元素就报错) a1.implicitly_wait(30) # 打开第一个百度热搜 a1.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[5]/div/div/div[4]/ul/li[1]/a").click() # 获取全部标签页句柄 a2 = a1.window_handles print(a2) # 切换到第一个句柄 a1.switch_to.window(a2[0]) # 打开第二个百度热搜 a1.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[5]/div/div/div[4]/ul/li[2]/a").click() # 获取当前标签页句柄 a2 = a1.current_window_handle print(f"当前控制标签页的句柄:{a2}") # 获取全部标签页句柄 a2 = a1.window_handles print(a2) # 切换到第一个句柄 a1.switch_to.window(a2[0]) a1.close()
14.多线程执行自动化
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By import threading class A1: # 设置、启动浏览器 def init(self): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) # 执行代码 def run(self): a1 = self.init() a1.get('https://www.baidu.com/') a1.implicitly_wait(30) a1.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[5]/div/div/div[4]/ul/li[1]/a").click() a2 = a1.window_handles a1.switch_to.window(a2[0]) a1.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[5]/div/div/div[4]/ul/li[2]/a").click() a2 = a1.current_window_handle print(f"当前控制标签页的句柄:{a2}") a2 = a1.window_handles print(a2) a1.switch_to.window(a2[0]) a1.close() s = A1() threading.Thread(target=s.run).start() threading.Thread(target=s.run).start() threading.Thread(target=s.run).start() threading.Thread(target=s.run).start()
15.警告框元素交互
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() a1.get('https://sahitest.com/demo/alertTest.htm') a1.find_element(By.XPATH, "/html/body/form/input[2]").click() time.sleep(2) # 获取弹窗内的文本内容 print(a1.switch_to.alert.text) # 点击弹窗确定按钮 a1.switch_to.alert.accept()
16.确认框弹窗交互
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # 引入新的包 # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() a1.get('https://sahitest.com/demo/confirmTest.htm') a1.find_element(By.XPATH, "/html/body/form/input[1]").click() time.sleep(2) # 点击弹窗确定按钮 # a1.switch_to.alert.accept() # 点击弹窗取消按钮 a1.switch_to.alert.dismiss()
17.提示框弹窗交互
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # 引入新的包 # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() a1.get('https://sahitest.com/demo/promptTest.htm') a1.find_element(By.XPATH, "/html/body/form/input[1]").click() time.sleep(2) # 弹窗输入内容 a1.switch_to.alert.send_keys("selenium") time.sleep(2) a1.switch_to.alert.accept()
18.iframe嵌套页面进入、退出
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # 引入新的包 # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() a1.get('https://sahitest.com/demo/iframesTest.htm') # 获取iframe元素 a2 = a1.find_element(By.XPATH, "/html/body/iframe") time.sleep(2) # 进入iframe嵌套页面 a1.switch_to.frame(a2) time.sleep(2) # 进入iframe页面操作元素点击 a1.find_element(By.XPATH, "/html/body/table/tbody/tr/td[1]/a[2]").click() # 退出iframe嵌套页面 a1.switch_to.default_content()
19.获取元素文本内容、是否可见
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # 引入新的包 # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() a1.get('https://baijiahao.baidu.com/s?id=1828082055905287531') # 获取元素文本 a2 = a1.find_element(By.XPATH, "/html/body/div[1]/div/div/div[2]/div[1]/div[1]/div[3]/div[1]/div[4]/span").text print(a2) # 元素是否可见 a3 = a1.find_element(By.XPATH, "/html/body/div[1]/div/div/div[2]/div[1]/div[1]/div[3]/div[1]/div[4]/span").is_displayed() print(a3)
20.网页前进、后退
import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # 引入新的包 # 设置、启动浏览器 def init(): q1 = Options() q1.add_argument("--no-sandbox") q1.add_experimental_option('detach', True) return webdriver.Chrome(service=Service('chromedriver-win64/chromedriver.exe'), options=q1) a1 = init() a1.get('https://baijiahao.baidu.com') a1.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[5]/div/div/form/span[1]/input").send_keys("selenium") time.sleep(2) a1.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[5]/div[2]/div/form/span[2]/input").click() time.sleep(2) # 网页后退 a1.back() time.sleep(2) # 网页前进 a1.forward()