Selenium学习
Selenium
- Selenium学习
- 前言
- 一、Selenium环境安装
- 二、创建浏览器,设置,打开
- 三、打开网页,关闭网页,浏览器
- 四、浏览器最大化,最小化
- 五、浏览器打开位置,尺寸
- 六、浏览器截图,网页刷新
- 七、元素定位
- 八、元素交互操作
- 九、元素定位-ID
- 十、元素定位-NAME
- 十一、元素定位-CLASS-NAME
- 十二、元素定位-TAG-NAME
- 十三、元素定位-LINK-TEXT
- 十四、元素定位-PARTIAL-LINK-TEXT
- 十五、元素定位-CSS-SELECTOR
- 十六、元素定位-XPATH
- 十七、元素定位-隐性等待
- 十八、单选,多选,下拉元素交互
- 十九、日期、评星、上传元素交互
- 二十、获取句柄,切换标签页
- 二十一、多线程执行自动化
- 二十二、警告框(alert)元素交互
- 二十三、确认框(confirm)元素交互
- 二十四、提示框(prompt)元素交互
- 二十五、iframe嵌套页面进入,退出
- 二十六、获取元素文本内容、是否可见
- 二十七、网页前进、后退
前言
总结自:B站-大发程序员-2025最新Selenium教程(Python 网页自动化测试脚本)
后面需要重新看一遍视频,后面需要添加图片,巩固。
提示:以下是本篇文章正文内容:
一、Selenium环境安装
python
# Selenium环境安装
# 1:浏览器安装(谷歌Chrome、火狐Firefox、微软Edge、苹果Safari等)
# Chrome官方下载地址:https://www.google.cn/chrome/
# 2:浏览器驱动安装(ChromeDriver、GeckoDriver、msedgedriver等)
# 官方最新驱动下载地址:
# https://googlechromelabs.github.io/chrome-for-testing/
# 注意:驱动版本号要和浏览器版本号对应符合(至少大版本对应),否则失效。
# 关闭自动更新:(防止更新导致驱动失效)
# 开始内搜索 services.msc 找到 Google更新组件全部禁用
# 安装三方库:selenium
二、创建浏览器,设置,打开
python
# 设置浏览器
# 禁用沙盒模式:add_argument('--no-sandbox')
# 保持浏览器打开状态:add_experimental_option('detach', True)
# 创建并启动浏览器:webdriver.Chrome()
# 导包:from selenium import webdriver
# 导包:from selenium.webdriver.chrome.options import Options
# 导包:from selenium.webdriver.chrome.service import Service
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.exe'), options=q1)
三、打开网页,关闭网页,浏览器
python
# 打开网页:get(url)
# 关闭当前标签页:close()
# 关闭浏览器:quit()
from selenium import webdriver # 用于操作浏览器
from selenium.webdriver.chrome.options import Options # 用于设置谷歌浏览器
from selenium.webdriver.chrome.service import Service # 用于管理驱动
import time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument('--no-sandbox')
q1.add_experimental_option('detach', True)
a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)
return a1
a1 = she()
# 打开指定网址
a1.get('http://baidu.com/')
time.sleep(3)
# 关闭当前标签页
a1.close()
# 退出浏览器并释放驱动
# a1.quit()
四、浏览器最大化,最小化
python
# 浏览器最大化 maximize_window()
# 浏览器最小化 minimize_window()
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
import time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
return a1
a1 = she()
a1.get('https://baidu.com')
time.sleep(2)
# 浏览器最大化
a1.maximize_window()
time.sleep(2)
# 浏览器最小化
a1.minimize_window()
time.sleep(2)
a1.maximize_window()
五、浏览器打开位置,尺寸
python
# 浏览器打开位置:set_window_position(x, y)
# 浏览器打开尺寸:set_window_size(width, height)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
import time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
return a1
a1 = she()
a1.get('https://baidu.com')
# 浏览器打开位置
a1.set_window_position(0, 0)
# 浏览器打开尺寸
a1.set_window_size(600, 600)
六、浏览器截图,网页刷新
python
# 浏览器截图:get_screenshot_as_file()
# 刷新当前网页:refresh()
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
import time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
return a1
a1 = she()
a1.get('https://baidu.com')
# 浏览器截图
a1.get_screenshot_as_file('1.png')
time.sleep(3)
# 刷新当前网页
a1.refresh()
七、元素定位
python
# 定位一个元素:a1.find_element(By.ID, 'kw')
# 定位多个元素:a1.find_elements(By.ID, 'kw')
# 在浏览器console中查找多个元素:document.getElementById('元素值')
# 元素定位导包:from selenium.webdriver.common.by import By
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
return a1
a1 = she()
a1.get('https://baidu.com')
# 定位一个元素(找到的话返回结果,找不到的话报错)
a2 = a1.find_element(By.ID, 'kw')
# 定位多个元素(找到的话返回列表形式,找不到的话返回空列表)
# a2 = a1.find_elements(By.ID, 'kw')
print(a2)
# <selenium.webdriver.remote.webelement.WebElement
# (session="34aabe5d1a9f9edcf8b44fd9ba975d35",
# element="f.5B9B6BD8B781DAC2435DCAF9581F43F9.d.73219D9CF3AB9EC867DC174B426EC4C4.e.5")>
八、元素交互操作
python
# 元素交互操作
# 元素点击:click()
# 元素输入:send_keys('dafait')
# 元素清空:clear()
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
return a1
a1 = she()
a1.get('https://baidu.com')
a2 = a1.find_element(By.ID, 'kw')
# 元素输入
a2.send_keys('dafait')
time.sleep(2)
# 元素清空
a2.clear()
time.sleep(2)
# 元素输入
a2.send_keys('dafait')
time.sleep(2)
a2 = a1.find_element(By.ID, 'su')
# 元素点击
a2.click()
九、元素定位-ID
python
# 元素定位-ID
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
return a1
a1 = she()
a1.get('https://baidu.com')
# 元素定位-ID
# 1, 通过ID定位元素,一般比较准确。
# 2, 并不是所有网页或者元素 都有ID值
a1.find_element(By.ID, 'kw').send_keys('dafait')
十、元素定位-NAME
python
# 元素定位-NAME
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
return a1
a1 = she()
a1.get('https://baidu.com')
# 元素定位-NAME
# 1, 通过name定位元素,一般比较准确。
# 2, 并不是所有网页或者元素 都有name值
a1.find_element(By.NAME, 'wd').send_keys('dafait')
十一、元素定位-CLASS-NAME
python
# 元素定位-CLASS_NAME
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
return a1
a1 = she()
a1.get('https://www.bilibili.com/')
# 元素定位-CLASS_NAME
# 1, class值不能有空格,否则报错
# 2, class值重复的有很多,需要切片
# 3, class值有的网站是随机的
a1.find_elements(By.CLASS_NAME, 'channel-link')[1].click()
十二、元素定位-TAG-NAME
python
# 元素定位-TAG_NAME
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
return a1
a1 = she()
a1.get('https://www.baidu.com/')
# 元素定位-TAG_NAME
# 1, 查找<开头标签名字>
# 2, 重复的标签名字特别多,需要切片
a1.find_elements(By.TAG_NAME, 'a')[3].click()
十三、元素定位-LINK-TEXT
python
# 元素定位-LINK_TEXT
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
return a1
a1 = she()
a1.get('https://www.bilibili.com/')
# 元素定位-LINK_TEXT
# 通过精准链接文本找到标签a的元素[精准文本定位]
# 有重复的文本,需要切片
a1.find_element(By.LINK_TEXT, '音乐').click()
十四、元素定位-PARTIAL-LINK-TEXT
python
# 元素定位-PARTIAL_LINK_TEXT
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
return a1
a1 = she()
a1.get('https://www.baidu.com/')
# 元素定位-PARTIAL_LINK_TEXT
# 通过模糊链接文本找到标签a的元素[模糊文本定位]
# 有重复的文本,需要切片
a1.find_elements(By.PARTIAL_LINK_TEXT, '3')[1].click()
十五、元素定位-CSS-SELECTOR
python
# 元素定位-CSS_SELECTOR
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
return a1
a1 = she()
a1.get('https://www.baidu.com/')
# 元素定位-CSS_SELECTOR
# 1,#id = 井号+id值 通过id定位
# 2,.class = 点+class值 通过class定位
# 3,不加修饰符 = 标签头 通过标签头定位
# 4,通过任意类型定位:"[类型='精准值']"
# 5,通过任意类型定位:"[类型*='模糊值']"
# 6,通过任意类型定位:"[类型^='开头值']"
# 7,通过任意类型定位:"[类型$'结尾值']"
# 以上这些方法都属于理论定位法
# 8,更简单的定位方式:在谷歌控制台直接复制 SELECTOR (各别元素定位值会比较长)
a1.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(5)").click()
十六、元素定位-XPATH
python
# 元素定位-XPATH
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
return a1
a1 = she()
a1.get('https://www.baidu.com/')
# 元素定位-XPATH
# 1, 复制谷歌浏览器 Xpath (通过属性+路径定位, 属性如果是随机的,可能定位不到)
# 2, 复制谷歌浏览器 Xpath 完整路径 (缺点是定位值 比较长,优点是基本100%准确)
a1.find_element(By.XPATH, '/html/body/div[1]/div[1]/div[3]/a[1]').click()
十七、元素定位-隐性等待
python
# 元素定位隐性等待:implicitly_wait(30)
# 演示地址:https://bahuyun.com/bdp/form/1327923698319491072
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
return a1
a1 = she()
a1.get('https://bahuyun.com/bdp/form/1327923698319491072')
# 元素定位隐性等待(多少秒内找到元素就立刻执行,没找到元素就报错)
a1.implicitly_wait(30)
a1.find_element(By.XPATH, '//*[@id="app"]/div/div/div[1]/div[1]/div[1]/i').click()
#大的,有名的网站,做过优化,有缓存策略,页面元素加载速度快
#一般的网站,如果页面元素还没有加载出来,就去查找这个元素,就会报错
#解决方法:等待一段时间再查找元素
十八、单选,多选,下拉元素交互
python
# 单选、多选、下拉元素交互
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
a1.implicitly_wait(30)
return a1
a1 = she()
a1.get('https://bahuyun.com/bdp/form/1327923698319491072')
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[2]/div/div[2]/div/div/div[3]').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[3]/div/div[2]/div/div/div[1]').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[3]/div/div[2]/div/div/div[2]').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[3]/div/div[2]/div/div/div[3]').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[4]/div/div[2]/div/div/div/select/option[2]').click()
十九、日期、评星、上传元素交互
python
# 日期、评星、上传元素交互
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
a1.implicitly_wait(30)
return a1
a1 = she()
a1.get('https://bahuyun.com/bdp/form/1327923698319491072')
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[2]/div/div[2]/div/div/div[3]').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[3]/div/div[2]/div/div/div[1]').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[3]/div/div[2]/div/div/div[2]').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[3]/div/div[2]/div/div/div[3]').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[4]/div/div[2]/div/div/div/select/option[2]').click()
a1.find_element(By.XPATH, '//*[@id="input-cG2LA_WGt0D0ic623V7ua"]').send_keys('0020251212')
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[6]/div/div[2]/div/div[1]/div[2]/div[5]/i').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[6]/div/div[2]/div/div[2]/div[2]/div[5]/i').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[7]/div/div[2]/div/div/div/div/div/input').send_keys(r'D:\xue1\Selenium\logo2.png')
二十、获取句柄,切换标签页
python
# 获取句柄、切换标签页
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
a1.implicitly_wait(30)
return a1
a1 = she()
a1.get('https://bahuyun.com/bdp/form/1327923698319491072')
for x in range(3):
time.sleep(2)
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[2]/div/div[2]/div/div/div[3]').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[3]/div/div[2]/div/div/div[1]').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[3]/div/div[2]/div/div/div[2]').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[3]/div/div[2]/div/div/div[3]').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[4]/div/div[2]/div/div/div/select/option[2]').click()
a1.find_element(By.XPATH, '//*[@id="input-cG2LA_WGt0D0ic623V7ua"]').send_keys('0020251212')
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[6]/div/div[2]/div/div[1]/div[2]/div[5]/i').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[6]/div/div[2]/div/div[2]/div[2]/div[5]/i').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[7]/div/div[2]/div/div/div/div/div/input').send_keys(r'D:\xue1\Selenium\logo2.png')
time.sleep(2)
a1.find_element(By.XPATH, '//*[@id="submit-button"]').click()
time.sleep(2)
a1.find_element(By.XPATH, '//*[@id="app"]/div/div/div[1]/div[2]/div[3]/button').click()
time.sleep(2)
# 获取全部标签页句柄
a2 = a1.window_handles
print(a2)
# 关闭之前标签页
a1.close()
# 通过句柄切换标签页
a1.switch_to.window(a2[1])
# 获取当前标签页句柄
a2 = a1.current_window_handle
print(f'当前控制标签页的句柄:{a2}')
二十一、多线程执行自动化
python
# 多线程执行自动化(四开浏览器)
# 导包:import threading
# 该教程内出现的演示地址均不能做任何破坏行为,否则后果自负###
# 该教程内出现的演示地址均不能做任何破坏行为,否则后果自负###
# 该教程内出现的演示地址均不能做任何破坏行为,否则后果自负###
# 该教程内出现的演示地址均不能做任何破坏行为,否则后果自负###
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 time
import threading
class A1:
def __init__(self, x, y):
self.x = x
self.y = y
# 设置浏览器、启动浏览器
def she(self):
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
a1.set_window_position(self.x, self.y)
a1.set_window_size(200, 400)
a1.implicitly_wait(30)
a1.get('https://bahuyun.com/bdp/form/1327923698319491072')
return a1
# 执行代码
def zhi(self):
a1 = self.she()
for x in range(3):
time.sleep(2)
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[2]/div/div[2]/div/div/div[3]').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[3]/div/div[2]/div/div/div[1]').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[3]/div/div[2]/div/div/div[2]').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[3]/div/div[2]/div/div/div[3]').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[4]/div/div[2]/div/div/div/select/option[2]').click()
a1.find_element(By.XPATH, '//*[@id="input-cG2LA_WGt0D0ic623V7ua"]').send_keys('0020251212')
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[6]/div/div[2]/div/div[1]/div[2]/div[5]/i').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[6]/div/div[2]/div/div[2]/div[2]/div[5]/i').click()
a1.find_element(By.XPATH, '//*[@id="my-node"]/div[7]/div/div[2]/div/div/div/div/div/input').send_keys(
r'D:\xue1\Selenium\logo2.png')
time.sleep(2)
a1.find_element(By.XPATH, '//*[@id="submit-button"]').click()
time.sleep(2)
a1.find_element(By.XPATH, '//*[@id="app"]/div/div/div[1]/div[2]/div[3]/button').click()
time.sleep(2)
a2 = a1.window_handles
a1.close()
a1.switch_to.window(a2[1])
s1 = A1(0, 0)
s2 = A1(800, 0)
s3 = A1(0, 500)
s4 = A1(800, 500)
threading.Thread(target=s1.zhi).start()
threading.Thread(target=s2.zhi).start()
threading.Thread(target=s3.zhi).start()
threading.Thread(target=s4.zhi).start()
二十二、警告框(alert)元素交互
python
# 警告框(alert)元素交互
# 演示地址:https://sahitest.com/demo/alertTest.htm
# 获取弹窗内的文本内容
# 点击弹窗确定按钮
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
a1.implicitly_wait(30)
return a1
a1 = she()
a1.get('https://sahitest.com/demo/alertTest.htm')
a1.find_element(By.XPATH, '/html/body/form/input[1]').clear()
time.sleep(1)
a1.find_element(By.XPATH, '/html/body/form/input[1]').send_keys('大发程序员')
time.sleep(1)
a1.find_element(By.XPATH, '/html/body/form/input[3]').click()
time.sleep(3)
# 获取弹窗内的文本内容
print(a1.switch_to.alert.text)
# 点击弹窗确定按钮
a1.switch_to.alert.accept()
二十三、确认框(confirm)元素交互
python
# 确认框(confirm)元素交互
# 演示地址:https://sahitest.com/demo/confirmTest.htm
# 点击弹窗取消按钮
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
a1.implicitly_wait(30)
return a1
a1 = she()
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()
二十四、提示框(prompt)元素交互
python
# 提示框(prompt)元素交互
# 演示地址:https://sahitest.com/demo/promptTest.htm
# 弹窗输入内容
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
a1.implicitly_wait(30)
return a1
a1 = she()
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('大发程序员')
time.sleep(2)
# 弹窗点击确定
a1.switch_to.alert.accept()
二十五、iframe嵌套页面进入,退出
python
# iframe嵌套页面进入、退出
# 演示地址:https://sahitest.com/demo/iframesTest.htm
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
a1.implicitly_wait(3)
return a1
a1 = she()
a1.get('https://sahitest.com/demo/iframesTest.htm')
# 获取iframe元素
a2 = a1.find_element(By.XPATH, '/html/body/iframe')
# 进入iframe嵌套页面
a1.switch_to.frame(a2)
time.sleep(2)
# 进入iframe页面操作元素点击
a1.find_element(By.XPATH, '/html/body/table/tbody/tr/td[1]/a[1]').click()
# 退出iframe嵌套页面(返回到默认页面)
a1.switch_to.default_content()
time.sleep(2)
a1.find_element(By.XPATH, '/html/body/input[2]').click()
二十六、获取元素文本内容、是否可见
python
# 获取元素文本内容、是否可见
# 演示地址:https://baijiahao.baidu.com/s?id=1814969914411764219
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
a1.implicitly_wait(3)
return a1
a1 = she()
a1.get('https://baijiahao.baidu.com/s?id=1814969914411764219')
# 获取元素文本内容 text
a2 = a1.find_element(By.XPATH, '//*[@id="ssr-content"]/div[2]/div[1]/div[1]/div[3]/div[1]/div[2]/p').text
print(a2)
# 元素是否可见 is_displayed()
a3 = a1.find_element(By.XPATH, '//*[@id="ssr-content"]/div[2]/div[1]/div[1]/div[3]/div[1]/div[2]/p').is_displayed()
print(a3)
二十七、网页前进、后退
python
# 网页前进、后退
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 time
# 设置浏览器、启动浏览器
def she():
q1 = Options()
q1.add_argument("--no-sandbox")
q1.add_experimental_option("detach", True)
a1 = webdriver.Chrome(service=Service(r'chromedriver.exe'), options=q1)
a1.implicitly_wait(3)
return a1
a1 = she()
a1.get('https://www.baidu.com/')
a1.find_element(By.XPATH, '//*[@id="kw"]').send_keys('dafait')
time.sleep(2)
a1.find_element(By.XPATH, '//*[@id="su"]').click()
time.sleep(2)
# 网页后退
a1.back()
time.sleep(2)
# 网页前进
a1.forward()