selenium实现浏览器自动化(玩起来了)

  • selenium模块简单介绍

    selenium是基于浏览器自动化的一个模块,可以便捷的获取网站中的动态加载数据,便捷的实现模拟登录。

  • 使用流程

    1、下载一个浏览器的驱动程序(笔者下载的是谷歌驱动程序)

      玩什么浏览器就下载什么样的驱动程序,下载路径与驱动程序和浏览器版本对应关系的链接如下:

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

      http://blog.csdn.net/huilan_same/article/details/51896672

    2、实例化一个浏览器对象

      需要实现导入包:

from selenium import webdriver
# 新版本中executable_path被放到了Service函数里,所以要先导入Service包
from selenium.webdriver.chrome.service import Service
# 实例化一个浏览器对象(传入浏览器驱动程序的路径)
s = Service(r'./chromedriver.exe')
driver = webdriver.Chrome(service=s)

    3、编写基于浏览器自动化的操作代码

      发起请求:

driver.get('https://www.baidu.com/')

      标签定位:

# 需要事先导包
from selenium.webdriver.common.by import By

# 根据id属性进行定位
search_input = driver.find_element(By.ID,'kw')

# 根据class属性进行定位
close_ad = driver.find_element(By.CLASS_NAME,'J_mm_dialog_close')

      标签交互:

# 往搜索框中输入关键字
search_input.send_keys('中北大学')

# 点击某按钮
close_ad.click()

      执行js程序:

# 滚动条缓慢下拉到最底
# 获取当前窗口总高度
js = "return action=document.body.scrollHeight"
# 初始化现在滚动条所在高度为0
height = 0
# 当前窗口总高度
new_height = driver.execute_script(js)

while height < new_height:
    # 将滚动条调整至页面底部
    for i in range(height, new_height, 100):
        driver.execute_script('window.scrollTo(0, {})'.format(i))
        time.sleep(0.8)
    height = new_height
    time.sleep(2)
    new_height = driver.execute_script(js)

      前进、后退:

# 回退
driver.back()

# 前进
driver.forward()

      关闭浏览器:

driver.close()
  • 代码演示
# -*- coding:utf-8 -*-
# @Time : 2022/1/23 0023 10:07
# @Author : Tzy0425
# @File : selenium基础用法.py

"""
老版本写法:
from selenium import webdriver
bro = webdriver.Chrome(executable_path='./chromedriver')
"""

from selenium import webdriver
# 新版本中executable_path被放到了Service函数里,所以要先导入Service包
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from lxml import etree
import time

# 实例化一个浏览器对象(传入浏览器驱动程序的路径)
s = Service(r'./chromedriver.exe')
driver = webdriver.Chrome(service=s)
driver.get('https://www.baidu.com/')
time.sleep(2)

# 标签定位
# close_ad = driver.find_element(By.CLASS_NAME,'J_mm_dialog_close')
# close_ad.click()
search_input = driver.find_element(By.ID,'kw')
# 标签交互
search_input.send_keys('中北大学')

# 点击搜索按钮
btn_search = driver.find_element(By.CLASS_NAME,'s_btn')
btn_search.click()
time.sleep(4)

# 执行js程序
# 下拉滚动条,向下翻一整页
# driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
# time.sleep(4)

# selenium控制浏览器滚动条缓慢下拉到最底
# 获取当前窗口总高度
js = "return action=document.body.scrollHeight"
# 初始化现在滚动条所在高度为0
height = 0
# 当前窗口总高度
new_height = driver.execute_script(js)

while height < new_height:
    # 将滚动条调整至页面底部
    for i in range(height, new_height, 100):
        driver.execute_script('window.scrollTo(0, {})'.format(i))
        time.sleep(0.8)
    height = new_height
    time.sleep(2)
    new_height = driver.execute_script(js)

# 浏览其它网页
driver.get('http://www.nuc.edu.cn/')
time.sleep(2)

# 回退
driver.back()
time.sleep(2)

# 前进
driver.forward()
time.sleep(2)

driver.close()

  运行效果如下:

  (我发誓在运行后,我的手已经完全脱离了键盘和鼠标!!!)

 

posted @ 2022-01-23 12:47  Sunshine_y  阅读(486)  评论(0编辑  收藏  举报