linux环境下使用lighthouse与selenium

2023-10-16 22:45

本文主要是介绍linux环境下使用lighthouse与selenium,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、安装谷歌浏览器、谷歌浏览器驱动、lighthouse

shell脚本

apt update && apt -y upgrade
apt install -y curl
curl -fsSL https://deb.nodesource.com/setup_18.x | bash
apt install -y nodejs
apt install -y npm
npm install -g lighthouse
apt-get install -y libxss1 libappindicator1 libindicator7 xvfb unzip vim
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
apt-get install -y apt-transport-https
#apt-get install -y google-chrome-stable=114.0.5735.90
wget http://dl.google.com/linux/deb/pool/main/g/google-chrome-unstable/google-chrome-unstable_114.0.5735.6-1_amd64.deb
apt-get install -f -y ./google-chrome-unstable_114.0.5735.6-1_amd64.deb
wget -N http://chromedriver.storage.googleapis.com/114.0.5735.90/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
mv -f chromedriver /usr/local/share/chromedriver
ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
sed -i '$s/$/ --no-sandbox/' /opt/google/chrome-unstable/google-chrome

1、安装lighthouse

指定安装node 18以上的版本

curl -fsSL https://deb.nodesource.com/setup_18.x | bash

安装nodejs

apt install -y nodejs

安装npm

apt install -y npm

安装lighthouse

npm install -g lighthouse

lighthouse Git地址:https://github.com/GoogleChrome/lighthouse
需要安装18以上版本的node,所以指定nodejs的版本
在这里插入图片描述

2、安装谷歌浏览器

添加 Google Chrome 的存储库

wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sh -c ‘echo “deb http://dl.google.com/linux/chrome/deb/ stable main” >> /etc/apt/sources.list.d/google.list’

安装 apt-transport-https 软件包以允许使用 HTTPS 传输数据

apt-get install -y apt-transport-https

指定安装114.0.5735.6-1版本的谷歌浏览器

wget http://dl.google.com/linux/deb/pool/main/g/google-chrome-unstable/google-chrome-unstable_114.0.5735.6-1_amd64.deb
apt-get install -f -y ./google-chrome-unstable_114.0.5735.6-1_amd64.deb

查看安装的浏览器版本

在这里插入图片描述

安装stable版本的谷歌

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb --安装最新的

apt-get install google-chrome-stable=91.0.4472.124-1 --指定版本安装

安装unstable版本的谷歌

wget http://dl.google.com/linux/deb/pool/main/g/google-chrome-unstable/google-chrome-unstable_73.0.3679.0-1_amd64.deb

找谷歌浏览器版本的地址

https://www.ubuntuupdates.org/package/google_chrome/stable/main/base/google-chrome-stable

验证下载地址是否可用

wget http://dl.google.com/linux/deb/pool/main/g/google-chrome-unstable/google-chrome-unstable_114.0.5735.6-1_amd64.deb
直接用http://dl.google.com/linux/deb/pool/main/g/google-chrome-unstable/google-chrome-unstable_114.0.5735.6-1_amd64.deb在浏览器打开,可以直接下载就是可用的

3、安装谷歌浏览器驱动

因为谷歌浏览器与谷歌浏览器驱动要版本匹配才能使用,所以指定安装的谷歌浏览器与驱动的版本。浏览器版本小于等于驱动版本。

指定浏览器版本与驱动版本

wget http://dl.google.com/linux/deb/pool/main/g/google-chrome-unstable/google-chrome-unstable_114.0.5735.6-1_amd64.deb
wget -N http://chromedriver.storage.googleapis.com/114.0.5735.90/chromedriver_linux64.zip

驱动版本查看地址

http://chromedriver.storage.googleapis.com/
下载地址就是http://chromedriver.storage.googleapis.com/ + key;根据自己系统下载想要版本的包
在这里插入图片描述

4、禁用沙箱模式

在谷歌浏览器可执行文件的最后一行后面加上–no-sandbox

sed -i ‘$s/$/ --no-sandbox/’ /opt/google/chrome-unstable/google-chrome

禁用沙箱模式的原因是,Lighthouse在某些情况下需要访问操作系统的底层资源或执行特权操作,而沙箱模式会限制这些访问和操作。禁用沙箱模式可以解决一些特定的问题,例如:

  1. 访问本地文件系统:Lighthouse可能需要读取或写入本地文件系统中的文件,例如加载本地CSS或JavaScript文件,或保存评估结果。沙箱模式会限制对本地文件系统的访问,因此禁用沙箱模式可以解决这个问题。

  2. 访问网络资源:Lighthouse可能需要访问本地网络资源,例如加载本地服务器上的网页或API。沙箱模式可能会限制对本地网络资源的访问,因此禁用沙箱模式可以解决这个问题。

二、linux环境下使用lighthouse

lighthouse cli

lighthouse “https://www.baidu.com/” --output=json --output-path=./report.json --only-categories=performance --throttling-method=provided --no-emulatedUserAgent --quiet --chrome-flags=“–headless --proxy-server=http://xx.xx.xx.xx:80” --no-enable-error-reporting

禁用错误报告

–no-enable-error-reporting,不然第一次执行的时候会询问你是否要报告

输出json报告与指定报告的保存路径

–output=json --output-path=./report.json

指定只测试性能部分

–only-categories=performance

指定不限制网速

–throttling-method=provided --no-emulatedUserAgent

安静模式运行,只输出最关键的结果信息

–quiet

无头模式,不会自动打开浏览器

–chrome-flags=“–headless”

正向代理,指定代理的地址

–chrome-flags=“–proxy-server=http://xx.xx.xx.xx:80”

在某些环境下,服务器无法直接访问外网需要通过代理去访问,则指定代理服务器

3、linux环境下使用selenium(python)

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
chrome_options = webdriver.ChromeOptions()
#指定代理
chrome_options.add_argument("--proxy-server=http://地址:端口号")
#无头模式
chrome_options.add_argument('--headless')
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
d = DesiredCapabilities.CHROME
d['goog:loggingPrefs'] = {'browser': 'ALL'}
driver = webdriver.Chrome(chrome_options=chrome_options,desired_capabilities=d)
driver.get("https://www.baidu.com/")
driver.quit()

这篇关于linux环境下使用lighthouse与selenium的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/221255

相关文章

在Android中使用WebView在线查看PDF文件的方法示例

《在Android中使用WebView在线查看PDF文件的方法示例》在Android应用开发中,有时我们需要在客户端展示PDF文件,以便用户可以阅读或交互,:本文主要介绍在Android中使用We... 目录简介:1. WebView组件介绍2. 在androidManifest.XML中添加Interne

Java Stream流与使用操作指南

《JavaStream流与使用操作指南》Stream不是数据结构,而是一种高级的数据处理工具,允许你以声明式的方式处理数据集合,类似于SQL语句操作数据库,本文给大家介绍JavaStream流与使用... 目录一、什么是stream流二、创建stream流1.单列集合创建stream流2.双列集合创建str

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

通过Docker容器部署Python环境的全流程

《通过Docker容器部署Python环境的全流程》在现代化开发流程中,Docker因其轻量化、环境隔离和跨平台一致性的特性,已成为部署Python应用的标准工具,本文将详细演示如何通过Docker容... 目录引言一、docker与python的协同优势二、核心步骤详解三、进阶配置技巧四、生产环境最佳实践

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结