网站怎么做弹窗,什么招聘网最好找工作,阜宁县住房和城乡建设局网站,哈尔滨建工建设集团在当今数字化的世界中#xff0c;网页自动化已经成为了不可或缺的技能。想象一下#xff0c;您可以通过编写代码#xff0c;让浏览器自动执行各种操作#xff0c;从点击按钮到填写表单#xff0c;从网页抓取数据到进行自动化测试。学习 Selenium#xff0c;这一功能… 在当今数字化的世界中网页自动化已经成为了不可或缺的技能。想象一下您可以通过编写代码让浏览器自动执行各种操作从点击按钮到填写表单从网页抓取数据到进行自动化测试。学习 Selenium这一功能强大的自动化工具将为您打开无尽的可能性。在本博客中您将深入探索 Selenium 的精髓学习如何构建稳定、高效的自动化脚本以及如何应用这些技能来提升工作效率、加速开发流程和实现可靠的网页交互。无论您是一名开发人员、自动化工程师还是对网页技术感兴趣的爱好者本博客将带您踏上一段令人激动的学习之旅释放出无限的可能性。准备好挑战传统、超越自我掌握 Selenium引领网页自动化的未来吗让我们一起探索吧 Selenium
简介
简介 Selenium是一个Web的自动化测试工具最初是为网站自动化测试而开发的类型像我们玩游戏用的按键精灵 可以按指定的命令自动操作不同是Selenium 可以直接运行在浏览器上它支持所有主流的浏览器包括PhantomJS这些无界面的浏览器。 Selenium 可以根据我们的指令让浏览器自动加载页面获取需要的数据甚至页面截屏或者判断网站上某些动作是否发生。 官网 selenium官网 https://selenium-python.readthedocs.io/index.html 注意 Selenium 自己不带浏览器不支持浏览器的功能它需要与第三方浏览器结合在一起才能使用 但是我们有时候需要让它内嵌在代码中运行所以我们可以用一个叫 PhantomJS 的工具代替真实的浏览器
安装
安装selenium
pip install selenium 安装ChromeDriver 国内源
https://registry.npmmirror.com/binary.html?pathchromedriver/ ChromeDriver 版本号要对应/帮助-关于Google Chrome——找到对应版本下载——下载的文件解压到python_version\Scripts 安装Firefox geckodriver 国内源
https://download-installer.cdn.mozilla.net/pub/firefox/releases/ Firefox geckodriver 安装firefox最新版本添加Firefox可执行程序到系统环境变量。记得关闭firefox的自动更新 将下载的geckodriver.exe 放到path路径下 D:\Python\python_version\
基础知识
基础操作
创建浏览器对象
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service Service(./chromedriver.exe)
chrome webdriver.Chrome(serviceservice) 打开页面
chrome.get(http://www.baidu.com) 打开本地页面
import os
file_pathfile:///os.path.abspath(./1.下拉菜单.html)
chrome.get(file_path) 获取页面html源码【换行】
page chrome.page_source 休眠
from time import sleep
sleep(8) 关闭浏览器
chrome.quit() 操作浏览器
窗口大小
chrome.maximize_window() #窗口最大化
chrome.set_window_size(600, 800) #设置窗口大小 前进和后退
chrome.forward()
chrome.back() 基础定位
定位元素
from selenium.webdriver.common.by import By
chrome.find_element(By.ID,su)
chrome.find_element(By.XPATH, //option[value10.69]).click() find_element(type,value) 一个元素 find_elements(type,value) 多个元素 By中参数选择 XPATH【xpath选择器】 ID【id属性】 NAME【name属性 】 CLASS_NAME 【class属性】 LINK_TEXT 【超链接的文本】 PARTIAL_LINK_TEXT partial link text TAG_NAME tag name CSS_SELECTOR css selector 操作元素 click 点击对象 send_keys 在对象上模拟按键输入 clear 清除对象的内容如果可以的话 基础示例
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from time import sleep
from selenium.webdriver.common.by import Byservice Service(./chromedriver.exe)
chrome webdriver.Chrome(serviceservice)
chrome.get(http://www.baidu.com)
sleep(3)
chrome.find_element(By.ID, kw).send_keys(CSDN)
sleep(3)
chrome.find_element(By.ID, su).click()
sleep(3) 常用操作
定位下拉菜单
注意 在定位下拉菜单时要先定位到父级元素然后再做一个模拟光标移动再点击所选项 页面代码
htmlheadmeta http-equivcontent-type contenttext/html;charsetutf-8 /titleLevel Locate/title script typetext/javascript srchttps://cdn.jsdelivr.net/npm/jquery1.12.4/dist/jquery.min.js/scriptlink hrefhttps://cdn.jsdelivr.net/npm/bootcss/v3.bootcss.com1.0.9/dist/css/bootstrap.min.css relstylesheet / /headbodyh3Level locate/h3div classspan3 col-md-3 div classwelldiv classdropdowna classdropdown-toggle data-toggledropdown href#Link1/aul classdropdown-menu rolemenu aria-labelledbydLabel iddropdown1 lia tabindex-1 hrefhttp://www.bjsxt.comAction/a/lilia tabindex-1 href#Another action/a/lilia tabindex-1 href#Something else here/a/lili classdivider/lilia tabindex-1 href#Separated link/a/li/ul/div /div /divdiv classspan3 col-md-3 div classwelldiv classdropdowna classdropdown-toggle data-toggledropdown href#Link2/aul classdropdown-menu rolemenu aria-labelledbydLabel lia tabindex-1 href#Action/a/lilia tabindex-1 href#Another action/a/lilia tabindex-1 href#Something else here/a/lili classdivider/lilia tabindex-1 href#Separated link/a/li/ul/div /div /div/bodyscript srchttps://cdn.jsdelivr.net/npm/bootcss/v3.bootcss.com1.0.9/dist/js/bootstrap.min.js/script/html核心代码
# 定位父级元素
chrome.find_element(By.LINK_TEXT, Link1).click()
sleep(4)
# 做一个移动光标的动作【模拟人工非必要】
menu chrome.find_element(By.LINK_TEXT, Action)
webdriver.ActionChains(chrome).move_to_element(menu).perform()
# 定位子集元素
menu.click() 示例代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from time import sleep
from selenium.webdriver.common.by import By
import osservice Service(./chromedriver.exe)
chrome webdriver.Chrome(serviceservice)
file_path file:/// os.path.abspath(./1.下拉菜单.html)
chrome.get(file_path)
sleep(3)
# 定位父级元素
chrome.find_element(By.LINK_TEXT, Link1).click()
sleep(4)
# 做一个移动光标的动作【模拟人工非必要】
menu chrome.find_element(By.LINK_TEXT, Action)
webdriver.ActionChains(chrome).move_to_element(menu).perform()
# 定位子集元素
menu.click()
sleep(4) 定位下拉框
简介 相比定位下拉菜单下拉框可以直接定位到元素 页面代码
html
body
select idShippingMethod onchangeupdateShipping(options[selectedIndex]); nameShippingMethodoption value12.51UPS Next Day Air $12.51/optionoption value11.61UPS Next Day Air Saver $11.61/optionoption value10.69UPS 3 Day Select $10.69/optionoption value9.03UPS 2nd Day Air $9.03/optionoption value8.34UPS Ground $8.34/optionoption value9.25USPS Priority Mail Insured $9.25/optionoption value7.45USPS Priority Mail $7.45/optionoption value3.20 selectedUSPS First Class $3.20/option
/select
/body
/html核心代码
# 定位到选择框并利用xpath进行选取
m chrome.find_element(By.ID, ShippingMethod)
m.find_element(By.XPATH, //option[value10.69]).click() 示例代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from time import sleep
from selenium.webdriver.common.by import By
import osservice Service(./chromedriver.exe)
chrome webdriver.Chrome(serviceservice)
file_path file:/// os.path.abspath(./3.drop_down.html)
chrome.get(file_path)
sleep(3)
# 定位到选择框并利用xpath进行选取
m chrome.find_element(By.ID, ShippingMethod)
m.find_element(By.XPATH, //option[value10.69]).click()
sleep(3)
chrome.quit()
定位层级内元素
简介 有时候我们定位一个元素定位器没有问题但一直定位不了这时候就要检查这个元素是否在一个frame中 页面代码
html
headmeta http-equivcontent-type contenttext/html;charsetutf-8/titleinner/title
/head
body
div classrow-fluiddiv classspan6 wellh3inner/h3iframe idf2 srchttps://cn.bing.com/ width700 height500/iframe/div
/div
/body
/htmlhtml
headmeta http-equivcontent-type contenttext/html;charsetutf-8/titleframe/titlescript typetext/javascript srchttps://cdn.jsdelivr.net/npm/jquery1.12.4/dist/jquery.min.js/scriptlink hrefhttp://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.cssrelstylesheet/
/headbody
div classrow-fluiddiv classspan10 wellh3frame/h3iframe idf1 src2.inner.html width800 , height600/iframe/div
/div
/body
script srchttps://cdn.jsdelivr.net/npm/bootcss/v3.bootcss.com1.0.8/dist/js/bootstrap.min.js/script
/html
/html核心代码
可以利用以下方法进入到内层元素【参数时id属性】
chrome.switch_to.frame(f1) 示例代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from time import sleep
from selenium.webdriver.common.by import By
import os# 定位层级内元素【三层】
service Service(./chromedriver.exe)
chrome webdriver.Chrome(serviceservice)
file_path file:/// os.path.abspath(./2.outer.html)
chrome.get(file_path)
sleep(3)
# 切换到frame里【根据id】
chrome.switch_to.frame(f1)
chrome.switch_to.frame(f2)
# 定位三层里的元素【www.baidu.com】
chrome.find_element(By.ID, sb_form_q).send_keys(CSDN)
chrome.find_element(By.ID, search_icon).click()
sleep(3) 处理弹窗
页面代码
!DOCTYPE html
html langen
headmeta charsetUTF-8titleThis is a page/title
/head
body
div idcontainerdiv stylefont: size 30px;Hello,Python Spider/div
/div
/body
scriptalert(这个是测试弹窗)/script
/html核心代码
# 定位弹出窗口并点击
chrome.switch_to.alert.accept() 示例代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from time import sleep
from selenium.webdriver.common.by import By
import osservice Service(./chromedriver.exe)
chrome webdriver.Chrome(serviceservice)
file_path file:/// os.path.abspath(./4.弹出框.html)
chrome.get(file_path)
sleep(3)
# 定位弹出窗口并点击
chrome.switch_to.alert.accept()
sleep(4)
chrome.quit() 拖拽元素
简介 拖拽元素如拖拽div标签【块级】
页面代码
!doctype html
html langen
headmeta charsetutf-8meta nameviewport contentwidthdevice-width, initial-scale1titlejQuery UI Draggable - Auto-scroll/titlelink relstylesheet hrefhttp://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.cssstyle#draggable, #draggable2, #draggable3 { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 0 10px 10px 0; }body {font-family: Arial, Helvetica, sans-serif;}table {font-size: 1em;}.ui-draggable, .ui-droppable {background-position: top;}/stylescript srchttps://code.jquery.com/jquery-1.12.4.js/scriptscript srchttps://code.jquery.com/ui/1.12.1/jquery-ui.js/scriptscript$( function() {$( #draggable ).draggable({ scroll: true });$( #draggable2 ).draggable({ scroll: true, scrollSensitivity: 100 });$( #draggable3 ).draggable({ scroll: true, scrollSpeed: 100 });} );/script
/head
body
div iddraggable classui-widget-contentpScroll set to true, default settings/p
/divdiv iddraggable2 classui-widget-contentpscrollSensitivity set to 100/p
/divdiv iddraggable3 classui-widget-contentpscrollSpeed set to 100/p
/div
div styleheight: 5000px; width: 1px;/div
/body
/html核心代码
# 定位要拖拽的元素
div1 chrome.find_element(By.ID, draggable)
div2 chrome.find_element(By.ID, draggable2)
div3 chrome.find_element(By.ID, draggable3)
sleep(3)
# 拖拽【把div1拖拽到div2处】
ActionChains(chrome).drag_and_drop(div1, div2).perform()
sleep(3)
# 拖拽【把div3向左/下各拖拽10px】
ActionChains(chrome).drag_and_drop_by_offset(div3, 10, 10).perform()
sleep(3) 示例代码
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service
from time import sleep
from selenium.webdriver.common.by import By
import os# 定位弹出框
service Service(./chromedriver.exe)
chrome webdriver.Chrome(serviceservice)
file_path file:/// os.path.abspath(./5.拖拽元素.html)
chrome.get(file_path)
sleep(3)
# 定位要拖拽的元素
div1 chrome.find_element(By.ID, draggable)
div2 chrome.find_element(By.ID, draggable2)
div3 chrome.find_element(By.ID, draggable3)
sleep(3)
# 拖拽【把div1拖拽到div2处】
ActionChains(chrome).drag_and_drop(div1, div2).perform()
sleep(3)
# 拖拽【把div3向左/下各拖拽10px】
ActionChains(chrome).drag_and_drop_by_offset(div3, 10, 10).perform()
sleep(3)
chrome.quit() 调用JS方法
简介 有时候我们需要控制页面滚动条上的滚动条但滚动条并非页面上的元素这个时候就需要借助js是来进行操作 注意 js都可以直接打开浏览器开发者工具去测试 控制台————输入js即可 核心代码
js window.scrollTo(100,400)# 拉动滚动条
driver.execute_script(js) 示例代码
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from time import sleepservice Service(./chromedriver.exe)
chrome webdriver.Chrome(serviceservice)
chrome.get(https://www.jd.com/)
# 拉动滚动条
js window.scrollTo(100,400)
chrome.execute_script(js)
sleep(3) 功能
等待元素
强制等待 作用当代码运行到强制等待这一行的时候无论出于什么原因都强制等待指定的时间需要通过time模块实现 优点简单 缺点无法做有效的判断会浪费时间
from time import sleep
sleep(3)
隐式等待 作用到了一定的时间发现元素还没有加载则继续等待我们指定的时间 如果超过了我们指定的时间还没有加载就会抛出异常如果没有需要等待的时候就已经加载完毕就会立即执行 优点 设置一次即可所有操作都会等待 缺点必须等待加载完成才能到后续的操作或者等待超时才能进入后续的操作
from selenium import webdriver
chrome.implicitly_wait(10)显示等待 作用指定一个等待条件并且指定一个最长等待时间会在这个时间内进行判断是否满足等待条件如果成立就会立即返回 如果不成立就会一直等待直到等待你指定的最长等待时间如果还是不满足就会抛出异常如果满足了就会正常返回 优点专门用于对指定一个元素等待加载完即可运行后续代码 缺点多个元素都需要要单独设置等待
from selenium.webdriver.support.wait import WebDriverWait
# 0.5指定检查条件的频率单位为秒。也就是每隔0.5秒检查一次条件是否满足
wait WebDriverWait(driver,10,0.5)
wait.until(EC.presence_of_element_located((By.CLASS_NAME, next))) 隐藏浏览器
实现
# 设置参数将浏览器隐藏起来(无头浏览器)
options ChromeOptions()
options.add_argument(--headless)
# 创建Chrome浏览器时加入参数
service Service(./chromedriver)
driver Chrome(serviceservice,optionsoptions) 代理模式
实现1
# 设置参数给浏览器设置代理
options ChromeOptions()
# options.add_argument(--proxy-serverhttp://ip:port)
options.add_argument(--proxy-serverhttp://221.199.36.122:35414)
# 设置驱动
service Service(./chromedriver)
# 启动Chrome浏览器
driver Chrome(serviceservice,optionsoptions) 实现2
from selenium.webdriver.common.proxy import ProxyType,Proxy
# 设置参数给浏览器设置代理
ip http://113.76.133.238:35680
proxy Proxy()
proxy.proxy_type ProxyType.MANUAL
proxy.http_proxy ip
proxy.ssl_proxy ip
# 关联浏览器
capabilities DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)# 设置驱动
service Service(./chromedriver)
# 启动Chrome浏览器
driver Chrome(serviceservice,desired_capabilitiescapabilities) 防检测设置
实现
from selenium.webdriver import Chrome
from selenium.webdriver import ChromeOptionsoptions ChromeOptions()
options.add_experimental_option(excludeSwitches, [enable-automation])
options.add_experimental_option(useAutomationExtension, False)chrome Chrome(chrome_optionsoptions)
chrome.execute_cdp_cmd(Page.addScriptToEvaluateOnNewDocument, {
source:
Object.defineProperty(navigator, webdriver, {
get: () false
})})chrome.get(http://httpbin.org/get)
info chrome.page_sourceprint(info)
sleep(20) 实战
虎牙
爬取英雄联盟全部分页的主播和对应的人气
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from lxml import etree
from selenium.webdriver.common.by import Byservice Service(../0.工具/chromedriver.exe)
chrome webdriver.Chrome(serviceservice)
# 设置隐式等待
chrome.implicitly_wait(5)
# 爬取英雄联盟页面的数据
chrome.get(https://www.huya.com/g/lol)
# 做一个循环退出条件是下一页没有数据
while True:# 分析数据e etree.HTML(chrome.page_source)names e.xpath(//i[classnick]/title) # 获取主播昵称person_nums e.xpath(//i[classjs-num]/text()) # 获取主播人气# 提取数据for n, p in zip(names, person_nums):print(f{n}————————————{p})try:# 找到下一页的按钮next_btn chrome.find_element(By.XPATH, //a[classlaypage_next])# 点击下一页next_btn.click()except Exception as e:break# if chrome.page_source.find(laypage_next) -1:# break# # 找到下一页的按钮# next_btn chrome.find_element(By.XPATH, //a[classlaypage_next])# # 点击下一页# next_btn.click()
chrome.quit()