> 文章列表 > 14.网络爬虫—selenium详讲

14.网络爬虫—selenium详讲

14.网络爬虫—selenium详讲

网络爬虫—selenium详讲

  • 一·selenium简介
    • Selenium 的优点
  • 二·安装模块
  • 三·设置浏览器驱动
      • 确认版本:
      • 查找对应驱动
      • 下载驱动
  • 四·使用模块
    • selenium选取元素方法
    • selenium嵌套页面元素定位
    • selenium网页下拉
    • selenium下拉表选择
    • selenium行为链
    • selenium等待
  • 五·错误解决方案
  • 六·结束语

前言
🏘️🏘️个人简介:以山河作礼。
🎖️🎖️:Python领域新星创作者,CSDN实力新星认证
📝​📝第一篇文章《1.认识网络爬虫》获得全站热榜第一,python领域热榜第一
🧾 🧾第四篇文章《4.网络爬虫—Post请求(实战演示)》全站热榜第八
🧾 🧾第八篇文章《8.网络爬虫—正则表达式RE实战》全站热榜第十二
🧾 🧾第十篇文章《10.网络爬虫—MongoDB详讲与实战》全站热榜第八,领域热榜第二
🧾 🧾第十三篇文章《13.网络爬虫—多进程详讲(实战演示)》全站热榜第十二
🎁🎁《Python网络爬虫》专栏累计发表十三篇文章,上榜五篇。欢迎免费订阅!欢迎大家一起学习,一起成长!!
💕💕悲索之人烈焰加身,堕落者不可饶恕。永恒燃烧的羽翼,带我脱离凡间的沉沦。

一·selenium简介

🧾 🧾Selenium是一个自动化测试工具,用于测试Web应用程序。它可以模拟用户在Web浏览器中的操作,如点击链接、填写表单、提交表单等。

  • Selenium的主要特点是灵活性和可扩展性,它可以与其他工具和框架集成,如JUnitTestNGMavenAnt等。
  • Selenium的核心组件包括Selenium IDESelenium WebDriverSelenium Grid
  • Selenium IDE是一个浏览器插件,用于录制和回放测试脚本`;
  • Selenium WebDriver是一个自动化测试框架,用于编写和执行测试脚本
  • Selenium Grid是一个分布式测试框架,用于在多台计算机上并行执行测试脚本
  • Selenium在Web应用程序测试领域具有广泛的应用和影响力。

Selenium 的优点

1.跨平台:Selenium 可以在多种操作系统和浏览器上运行,可以在不同的环境中进行测试。
2. 灵活性:Selenium 提供了多种 API 和工具,可以根据具体需求进行定制化开发,满足不同的测试需求。
3. 易于学习:Selenium 的 API 简单易懂,学习成本较低,而且有丰富的文档和社区支持
4. 可扩展性:Selenium 可以与其他测试工具和框架集成,例如 TestNG、JUnit 等,从而实现更加完善的测试流程。

二·安装模块

🧾 在pycharm的终端输入安装模块的命令

 pip install selenium

14.网络爬虫—selenium详讲
可以使用清华源安装加快安装速度:

 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple selenium

三·设置浏览器驱动

确认版本:

🧾 博主使用的是谷歌浏览器,确认版本的方法如下:

点击右上的三个点->点击浏览器的设置->点击关于谷歌->查看版本(版本 112.0.5615.50(正式版本) (64 位))

14.网络爬虫—selenium详讲
14.网络爬虫—selenium详讲

查找对应驱动

http://chromedriver.storage.googleapis.com/index.html

查找和浏览器版本最近的一个驱动版本(版本向下兼容)

14.网络爬虫—selenium详讲
博主的浏览器版本是:版本 112.0.5615.50(正式版本) (64 位)
所以下载的是112.0.5616.28 向下兼容,所以可以正常使用。

下载驱动

下载chromedriver_win32.zip

系统是 64 位的,也可以使用 32 位的 ChromeDriver。因为 ChromeDriver
只是一个独立的可执行文件,它与您的操作系统架构无关。只要您的 Chrome 浏览器和 ChromeDriver
版本匹配,就可以在任何系统上运行 ChromeDriver。

解压chromedriver.exe 存放到一个位置(后续会使用)

注意点:
浏览器版本更新后,对应的驱动也需要更新才可以使用(可以去查找一下如何取消浏览器更新)

四·使用模块

🧾 我们以打开百度浏览器为例:

from selenium.webdriver.chrome.service import Service
from selenium import webdriverservice = Service(executable_path='D:\\chorm\\chromedriver_win32/chromedriver.exe')
driver = webdriver.Chrome(service=service)driver.set_window_size(1100, 850)  # 设置打开的窗口大小
driver.get('https://www.baidu.com/?tn=02003390_84_hao_pg&')  # 选择自动化操作页面input()  # 加入input()是为了让程序暂停,等待用户输入任意字符后才继续执行下一步操作。

运行结果:
14.网络爬虫—selenium详讲

运行成功后,他会帮我们自动打开百度网页。我们可以看见上方出现chrome正受到自动测试软件的控制
14.网络爬虫—selenium详讲

加入input()是为了让程序暂停,等待用户输入任意字符后才继续执行下一步操作。这样做是为了防止程序执行完毕后自动关闭浏览器窗口,让用户有足够的时间观察程序的执行结果或手动进行后续操作。

selenium选取元素方法

  • find_element获取满足条件的第一个元素
  • find_elements获取满足条件的所有元素
序号 方法 描述
1 find_element_by_id() 通过ID定位元素
2 find_element_by_name() 通过name定位元素
3 find_element_by_class_name() 通过类样式名称定位元素
4 find_element_by_tag_name() 通过标签名称定位元素
5 find_element_by_link_text() 通过链接定位元素(a标签)
6 find_element_by_css_selector() 通过CSS定位元素
7 find_element_by_xpath() 通过xpath语法来获取元素

示例:

from selenium.webdriver.common.by import Bydriver.find_element(By.ID, 'kw').send_keys('飞机')

在百度输入框自动输入夕阳两个字。

from selenium.webdriver.chrome.service import Service
from selenium import webdriverservice = Service(executable_path='D:\\chorm\\chromedriver_win32/chromedriver.exe')
driver = webdriver.Chrome(service=service)driver.set_window_size(1100, 850)  # 设置打开的窗口大小
driver.get('https://www.baidu.com/?tn=02003390_84_hao_pg&')  # 选择自动化操作页面from selenium.webdriver.common.by import Bydriver.find_element(By.ID, 'kw').send_keys('夕阳')input()  # 加入input()是为了让程序暂停,等待用户输入任意字符后才继续执行下一步操作。

接下来我们使用代码来操作,让它执行搜索功能:

14.网络爬虫—selenium详讲

selenium嵌套页面元素定位

import timefrom selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import Byservice = Service(executable_path='D:\\chorm\\chromedriver_win32/chromedriver.exe')
driver = webdriver.Chrome(service=service)driver.get('https://tieba.baidu.com/')
# 方法1 直接使用嵌套的网页进行使用# 跳转到嵌套网页
# 选取嵌套网页元素
element = driver.find_element(By.ID, 'iframeu6739266_0')
# 切换网页
driver.switch_to.frame(element)driver.find_element(By.ID, 'title0').click()
time.sleep(5)

14.网络爬虫—selenium详讲

selenium网页下拉

from selenium.webdriver.chrome.service import Service
from selenium import webdriver
import timeservice = Service(executable_path='D:\\chorm\\chromedriver_win32/chromedriver.exe')
driver = webdriver.Chrome(service=service)driver.get('https://www.duitang.com/category/?cat=wallpaper')s = 0
n = 100
for i in range(s, n, 5):  # 实现网页滑动下拉js = 'window.scrollTo(0,%s)' % (i * 100)driver.execute_script(js)time.sleep(0.5)

运行这段程序后,会自动打开堆糖网页,并且实现自动下拉页面,方便我们浏览。
14.网络爬虫—selenium详讲

selenium下拉表选择

from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import Byservice = Service(executable_path='F:\\chrom\\chromedriver.exe')
driver = webdriver.Chrome(service=service)driver.get('https://kyfw.12306.cn/otn/regist/init')from selenium.webdriver.support.ui import Selectselect_element = driver.find_element(By.ID,'cardType')select = Select(select_element)

14.网络爬虫—selenium详讲

selenium行为链

Selenium行为链(ActionChains)是Selenium中的一个Python库,它允许我们模拟用户的交互行为,例如鼠标移动、单击、双击、右键单击等。使用行为链,我们可以创建一个动作序列,然后将其执行在我们的Web应用程序上,从而模拟用户的行为。

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import Byservice = Service(executable_path='F:\\chrom\\chromedriver.exe')
driver = webdriver.Chrome(service=service)driver.get('https://www.baidu.com/')action = ActionChains(driver)  # 在driver创建行为链对象inp = driver.find_element(By.ID, 'kw')  # 获取到输入框位置
action.move_to_element(inp)  # 把鼠标移动到输入框
action.send_keys_to_element(inp, '百度贴吧')  # 模拟输入btn = driver.find_element(By.ID, 'su')  # 获取搜索按钮
action.move_to_element(btn)  # 移动鼠标到搜索按钮
action.click(btn)  # 模拟点击action.perform()  # 注意记得写 执行行为

14.网络爬虫—selenium详讲

selenium等待

from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located as PEservice = Service(executable_path='F:\\chrom\\chromedriver.exe')
driver = webdriver.Chrome(service=service)driver.get('https://movie.douban.com/typerank?type_name=%E5%89%A7%E6%83%85&type=11&interval_id=100:90&action=')
# 肖申克的救赎
# 需要等待页面加载完成再执行打印
# time.sleep(5)
# print(driver.page_source)wait = WebDriverWait(driver, 10)  # 等待10秒,有数据就进行操作,没有就报错
wait.until(PE((By.CLASS_NAME, 'rank-num'))).click()print(driver.page_source)

14.网络爬虫—selenium详讲

14.网络爬虫—selenium详讲

五·错误解决方案

( 1)109.0.5414.75

selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 86
Current browser version is 109.0.5414.75 with binary path C:\\ProgramFiles\\Google\\Chrome\\Application\\chrome.exe

需要指定驱动位置

1.将驱动放置在代码的同级目录
2. 直接指定驱动的位置

(2) 浏览器闪退

selenium代码没有进行阻塞 和浏览器的驱动版本是有关系

如果想要阻塞 可以在代码最后加上sleep(10) 或者是input()

(3)运行代码出现警告

F:\\pythonSpider2208\\day14-selenium\\selenium自动化.py:5: DeprecationWarning: executable_path has been deprecated, please pass in a Service objectdriver = webdriver.Chrome(executable_path=r'F:\\chrom\\chromedriver.exe')

需要我们更新代码,使用新的方式指定驱动位置(代码依然是可以正常使用的)

(4) 元素选择异常

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="wd"]"}

(5)元素没有选择到

检测value参数是否正确

六·结束语

👉👉本专栏所有文章是博主学习笔记,仅供学习使用,爬虫只是一种技术,希望学习过的人能正确使用它。
博主也会定时一周三更爬虫相关技术更大家系统学习,如有问题,可以私信我,没有回,那我可能在上课或者睡觉,写作不易,感谢大家的支持!!🌹🌹🌹