当前位置: 首页 > news >正文

【Python】selenium工具

目录

1. 安装

2. 测试

3. 无头浏览器

4. 元素定位

5. 页面滑动

6. 按键、填写登录表单

7. 页面切换


Selenium是Web的自动化测试工具,为网站自动化测试而开发,Selenium可以直接运行在浏览器上,它支持所有主流的浏览器,可以接收指令,让浏览器自动加载界面,获取需要的数据,页面截屏。

1. 安装

浏览器:谷歌、火狐、Edge,这些浏览器的内核都是google

打开浏览器设置,查看浏览器版本

打开chromedriver下载网页,选择一个和浏览器内核版本最接近的一个版本:CNPM Binaries Mirrorhttps://registry.npmmirror.com/binary.html?path=chromedriver/点击进入,此处以windows环境为示例:

下载安装包,并解压这个文件,得到这个exe文件

如果你是使用PyCharm自带的Python解释器,那么你需要将这个文件放入你的PyCharm文件的bin目录下,例如:C:\...\PyCharm Community Edition 2022.1.3\bin

如果你是通过PyCharm使用Anaconda虚拟环境,那么你需要将这个文件放入你的Anaconda文件的Scripts目录下,例如:C:\...\Anaconda3\Scripts

添加环境变量,如果你已经是使用过PyCharm的用户,那么你的PyCharm大概率是已经添加进入环境变量了,此时你不用再添加环境变量

2. 测试

写一个访问浏览器页面的测试代码,首先下载selenium模块

from selenium import webdriver
import time# 这两个方法二选一,webdriver.Chrome()会真的打开一个浏览器
# driver = webdriver.PhantomJS()
driver = webdriver.Chrome()# 访问浏览器网址
driver.get('https://www.douban.com/')# 截图保存图片
driver.save_screenshot("首页.png")# 页面停留时间
time.sleep(5)# 退出当前页面
driver.close()# 退出浏览器
driver.quit()

运行成功会打开浏览器豆瓣首页网址并停留5秒,拿到首页截图。

3. 无头浏览器

无头浏览器不会打开浏览器页面,但会访问网页,适用于Linux环境

最新的selenium已经放弃了Phantomjs,直接将无头浏览器的逻辑进行了整合

from selenium import webdriver# 无头
from selenium.webdriver.chrome.options import Options# 配置参数
opt = Options()
opt.add_argument("--headless")
opt.add_argument("--disable-gpu")# 把参数配置给浏览器
driver = webdriver.Chrome(options=opt)driver.get("https://www.douban.com/")
driver.save_screenshot("./selenium_test/首页1.png")
driver.close()
driver.quit()

4. 元素定位

from selenium import webdriver
import time
from lxml import etree# 无头
from selenium.webdriver.chrome.options import Options# 元素定位
from selenium.webdriver.common.by import By# 配置参数
opt = Options()
opt.add_argument("--headless")
opt.add_argument("--disable-gpu")# 把参数配置给浏览器
driver = webdriver.Chrome(options=opt)driver.get("https://book.douban.com/")# 截屏当前页面
driver.save_screenshot("./selenium_test/tv.png")# 获取前端代码
test = driver.page_source
# print(test)# html = etree.HTML(test)
# xpath_result = html.xpath('//*[@id="content"]/div/div[1]/div[1]/div[2]/div[1]/div/ul[2]/li')
# print(xpath_result)
# print(len(xpath_result))
# for i in xpath_result:
#     print(i.xpath('.//div[@class="info"]//a/@title'))# 元素定位
xpath_result = driver.find_element(By.XPATH, '//*[@id="content"]/div/div[1]/div[1]/div[2]/div[1]/div/ul[2]/li[1]')
print(xpath_result)
print(xpath_result.text)time.sleep(3)
driver.close()
driver.quit()

5. 页面滑动

from selenium import webdriver
import timedriver = webdriver.Chrome()driver.get("https:/www.douban.com/")time.sleep(2)
js = 'window.scrollTo(0, 10000)'   # 向下滑
# js = 'window.scrollTo(10000, 0)' # 向左滑
# js = 'window.scrollTo(10000, 10000)' # 向左并向下滑
driver.execute_script(js)
time.sleep(2)       # 向上海
js = 'window.scrollTo(0, -10000)'
driver.execute_script(js)time.sleep(5)driver.close()
driver.quit()

6. 按键、填写登录表单

from selenium import webdriver
import time
from selenium.webdriver.common.by import Bydriver = webdriver.Chrome()try:driver.get('https://book.douban.com/')time.sleep(3)btn = driver.find_element(By.LINK_TEXT, '登录/注册')btn.click()# url = driver.current_url# driver.get(url)driver.find_element(By.XPATH, '//*[@id="account"]/div[2]/div[2]/div/div[1]/ul[1]/li[2]').click()time.sleep(2)# 填写登录表单driver.find_element(By.XPATH, '//*[@id="username"]').send_keys('12345678')driver.find_element(By.XPATH, '//*[@id="password"]').send_keys('202125DOUBAN')time.sleep(2)driver.find_element(By.XPATH, '//*[@id="account"]/div[2]/div[2]/div/div[2]/div[1]/div[4]/a').click()time.sleep(10)driver.close()driver.quit()except:print(Exception)

7. 页面切换

不同的网站有不同的应有场景,有些网站不会新生成页面,有些网站可以自动跳转

from selenium import webdriver
from selenium.webdriver.common.by import By
from lxml import etree
import timeurl = 'https://www.bilibili.com/'
driver = webdriver.Chrome()
driver.get(url)html = driver.page_source
html = etree.HTML(html)href = html.xpath('//*[@id="i_cecream"]/div[2]/main/div[2]/div/div[1]/div[5]/div/div[2]/a/@href')
print(href)
time.sleep(3)driver.find_element(By.XPATH, '//*[@id="i_cecream"]/div[2]/main/div[2]/div/div[1]/div[5]/div/div[2]/a').click()time.sleep(3)
# 获取当前所有窗口
current_windows = driver.window_handles
# 根据窗口索引进行切换
driver.switch_to.window(current_windows[0])     # 从 0 下标开始time.sleep(3)
driver.close()
time.sleep(3)
driver.quit()

http://www.lryc.cn/news/64038.html

相关文章:

  • 实验六~Web事件处理与过滤器
  • 刷题4.28
  • 做了一年csgo搬砖项目,还清所有债务:会赚钱的人都在做这件事 !
  • 线性回归模型(7大模型)
  • VP记录:Codeforces Round 868 (Div. 2) A~D
  • 【VQ-VAE-2论文精读】Generating Diverse High-Fidelity Images with VQ-VAE-2
  • 并发编程基石:管程
  • 电路中噪声来源
  • JAVASE的全面总结
  • 关于repeater录制的流量子调用的identity中带有~S的情况
  • Java面试题队列
  • 大型Saas系统的权限体系设计(二)
  • HTML(四) -- 多媒体设计
  • 设置苹果电脑vsode在新窗口中打开文件
  • 第二章创建模式—单例设计模式
  • 数据结构学习记录——堆的插入(堆的结构类型定义、最大堆的创建、堆的插入:堆的插入的三种情况、哨兵元素)
  • netperf测试
  • ORACLE常用语句
  • [论文笔记]C^3F,MCNN:图片人群计数模型
  • HCIP-7.2VLAN间通信单臂、多臂、三层交换方式学习
  • PHP快速入门17-用spl_autoload_register实现类的自动加载
  • 【黑马程序员 C++教程从0到1入门编程】【笔记8】 泛型编程——模板
  • 分享10个精美可视化模板,解决95%的大屏需求!
  • 好用的项目管理软件的具体功能有哪些
  • < 每日小技巧: 基于Vue状态的过渡动画 - Transition 和 TransitionGroup>
  • vmware安装redhat 8
  • OpenCV C++案例实战三十一《动态时钟》
  • 字节后端入门 - Go 语言原理与实践
  • 锂电材料浆料匀浆搅拌设备轴承经常故障如何处理?
  • 设计模式——设计模式介绍和单例设计模式