linux环境下使用lighthouse与selenium

2023-10-13 19:12

本文主要是介绍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/205291

相关文章

Qt之QMessageBox的具体使用

《Qt之QMessageBox的具体使用》本文介绍Qt中QMessageBox类的使用,用于弹出提示、警告、错误等模态对话框,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.简单介绍3.常见函数4.按钮类型(QMessage::StandardButton)5.分步骤实现弹窗6.总结1.引言

Python使用Reflex构建现代Web应用的完全指南

《Python使用Reflex构建现代Web应用的完全指南》这篇文章为大家深入介绍了Reflex框架的设计理念,技术特性,项目结构,核心API,实际开发流程以及与其他框架的对比和部署建议,感兴趣的小伙... 目录什么是 ReFlex?为什么选择 Reflex?安装与环境配置构建你的第一个应用核心概念解析组件

Qt中Qfile类的使用

《Qt中Qfile类的使用》很多应用程序都具备操作文件的能力,包括对文件进行写入和读取,创建和删除文件,本文主要介绍了Qt中Qfile类的使用,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.QFile文件操作3.演示示例3.1实验一3.2实验二【演示 QFile 读写二进制文件的过程】4.

spring security 超详细使用教程及如何接入springboot、前后端分离

《springsecurity超详细使用教程及如何接入springboot、前后端分离》SpringSecurity是一个强大且可扩展的框架,用于保护Java应用程序,尤其是基于Spring的应用... 目录1、准备工作1.1 引入依赖1.2 用户认证的配置1.3 基本的配置1.4 常用配置2、加密1. 密

WinForms中主要控件的详细使用教程

《WinForms中主要控件的详细使用教程》WinForms(WindowsForms)是Microsoft提供的用于构建Windows桌面应用程序的框架,它提供了丰富的控件集合,可以满足各种UI设计... 目录一、基础控件1. Button (按钮)2. Label (标签)3. TextBox (文本框

使用Vue-ECharts实现数据可视化图表功能

《使用Vue-ECharts实现数据可视化图表功能》在前端开发中,经常会遇到需要展示数据可视化的需求,比如柱状图、折线图、饼图等,这类需求不仅要求我们准确地将数据呈现出来,还需要兼顾美观与交互体验,所... 目录前言为什么选择 vue-ECharts?1. 基于 ECharts,功能强大2. 更符合 Vue

如何合理使用Spring的事务方式

《如何合理使用Spring的事务方式》:本文主要介绍如何合理使用Spring的事务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、底层构造1.1.事务管理器1.2.事务定义信息1.3.事务状态1.4.联系1.2、特点1.3、原理2. Sprin

Vue中插槽slot的使用示例详解

《Vue中插槽slot的使用示例详解》:本文主要介绍Vue中插槽slot的使用示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、插槽是什么二、插槽分类2.1 匿名插槽2.2 具名插槽2.3 作用域插槽三、插槽的基本使用3.1 匿名插槽

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

PyQt5 QDate类的具体使用

《PyQt5QDate类的具体使用》QDate是PyQt5中处理日期的核心类,本文主要介绍了PyQt5QDate类的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录核心功能常用方法及代码示例​1. 创建日期对象​2. 获取日期信息​3. 日期计算与比较​4. 日