首先我们需要了解的是,jenkins是一个单独的环境,所以我们的python跟chrome都需要搭建在我们的jenkins容器里面,接下来我将跟大家一起来搭建,这其中也遇到了很多问题,不过最后都解决了,不多说,上干货
需要你已经搭建了jenkins容器以及python环境
https://blog.csdn.net/weixin_42382016/article/details/122366318
https://blog.csdn.net/weixin_42382016/article/details/122366740
下载chrome包,选择linux版本的.deb包
https://www.google.com/intl/zh-CN/chrome/
下载完之后,复制到服务器,由于我的jenkins数据是挂在的 只需要放到var/jenkins_home 既可
chmod +x google-chrome-stable_current_amd64.deb
dpkg -i google-chrome-stable_current_amd64.deb
google-chrome-stable depends on libxfixes3; however:
Package libxfixes3 is not installed.
google-chrome-stable depends on libxkbcommon0 (>= 0.4.1); however:
Package libxkbcommon0 is not installed.
google-chrome-stable depends on libxrandr2; however:
Package libxrandr2 is not installed.
google-chrome-stable depends on xdg-utils (>= 1.0.2); however:
Package xdg-utils is not installed.
dpkg: error processing package google-chrome-stable (--install):
dependency problems - leaving unconfigured
Errors were encountered while processing:
google-chrome-stable
这个时候我们需要安装多余依赖包
sudo apt-get -f install
重新安装 这次我们用sudo安装
sudo dpkg -i google-chrome-stable_current_amd64.deb
google-chrome --version
下载对应版本驱动
https://chromedriver.storage.googleapis.com/index.html?path=97.0.4692.71/
unzip chromedriver_linux64.zip
chmod +x chromedriver
mv chromedriver /usr/bin/
cp /usr/bin/chromedriver /usr/local/bin/
google-chrome
发现报错
google-chrome 报错
ERROR:nacl_helper_linux.cc(308)] NaCl helper process running without a sandbox!
google-chrome --no-sandbox
这个时候我们运行google-chrome 发现报错
root@4f0994c37a27:/usr/bin# google-chrome
bash: /usr/bin/google-chrome: Permission denied
这个我上网查资料发现是权限不足,所以重新给他赋值权限
进入/usr/bin 目录重新给google-chrome赋值
chmod 777 google-chrome
然后暂时先不管这个,没有什么大问题了
找出驱动位置
which chromedriver
然后 vi /etc/profile
#chromedriver
export PATH=$PATH:/usr/local/bin/chromedriver
添加完成之后保存
source /etc/profile
主要的是:
chrome_options = Options() chrome_options.add_argument(‘no-sandbox’)
chrome_options.add_argument(“headless”) # => 为Chrome配置无头模式
chrome_options.add_argument(‘disable-dev-shm-usage’)
chrome_options.add_argument(‘disable-gpu’) # 谷歌文档提到需要加上这个属性来规避bug
demo_ui.py
# -*- coding: utf-8 -*-
# @Time : 2022/1/11 13:30
# @Author : Limusen
# @File : demo_ui
from selenium import webdriver
option = webdriver.ChromeOptions()
# 无头模式
option.add_argument('headless')
# 沙盒模式运行
option.add_argument('no-sandbox')
# 大量渲染时候写入/tmp而非/dev/shm
option.add_argument('disable-dev-shm-usage')
# 指定驱动路径
browser = webdriver.Chrome( options=option)
# 访问百度
browser.get('http://www.baidu.com/')
# 打印标题
print(browser.title)
# 关闭浏览器
browser.quit()
发现可以正常运行
然后我们执行自己的脚本就ok了。注意需要执行完一个case之后需要关闭或者刷新浏览器 不然会报错
优化一下驱动类,在根据不同的环境运行执行不同的driver,并且linux下需要配置无头模式
# -*- coding: utf-8 -*-
# @Time : 2022/1/4 19:44
# @Author : Limusen
# @File : browser_utils
# 封装驱动类
import os
import sys
import warnings
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from common.config_utils import local_config
from common.log_utils import logger
current_path = os.path.dirname(os.path.abspath(__file__))
system_driver = sys.platform
class BrowserUtils:
def __init__(self):
# 去除控制台警告
warnings.filterwarnings("ignore", category=DeprecationWarning)
self.driver_name = local_config.driver_name
def get_driver(self):
if self.driver_name.lower() == 'chrome':
logger.info("当前正在打开:%s" % self.driver_name)
return self.__get_chrome_driver()
elif self.driver_name.lower() == 'firefox':
logger.info("当前正在打开:%s" % self.driver_name)
return self.__get_firefox_driver()
elif self.driver_name.lower() == 'edge':
logger.info("当前正在打开:%s" % self.driver_name)
return self.__get_edge_driver()
def __get_chrome_driver(self):
# 先封装简易代码
# 加入系统环境判断
if system_driver.lower() == "darwin":
chrome_options = Options()
chrome_options.add_argument('--disable-gpu') # 谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('lang=zh_CN.UTF-8') # 设置默认编码为utf-8
chrome_options.add_experimental_option('useAutomationExtension', False) # 取消chrome受自动控制提示
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation']) # 取消chrome受自动控制提示
"""如果是mac系统执行这个驱动"""
chrome_path = os.path.join(current_path, '..','webdriver', 'chrome', 'chromedriver')
driver = webdriver.Chrome(executable_path=chrome_path, options=chrome_options)
return driver
elif system_driver.lower() == "win32":
chrome_options = Options()
chrome_options.add_argument('--disable-gpu') # 谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('lang=zh_CN.UTF-8') # 设置默认编码为utf-8
chrome_options.add_experimental_option('useAutomationExtension', False) # 取消chrome受自动控制提示
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation']) # 取消chrome受自动控制提示
chrome_path = os.path.join(current_path, '..', 'webdriver', 'chrome', 'chromedriver93.exe')
driver = webdriver.Chrome(executable_path=chrome_path, options=chrome_options)
return driver
elif system_driver.lower() == "linux":
chrome_options = Options()
chrome_options.add_argument('no-sandbox')
chrome_options.add_argument("headless") # => 为Chrome配置无头模式
chrome_options.add_argument('disable-dev-shm-usage')
chrome_options.add_argument('disable-gpu') # 谷歌文档提到需要加上这个属性来规避bug
driver = webdriver.Chrome(options=chrome_options)
return driver
def __get_firefox_driver(self):
if system_driver.lower() == "darwin":
firefox_path = os.path.join(current_path, '..', 'webdriver', 'firefox', 'geckodriver')
driver = webdriver.Firefox(executable_path=firefox_path)
logger.info('初始化Firefox浏览器并启动')
return driver
else:
firefox_path = os.path.join(current_path, '..', 'webdriver', 'firefox', 'geckodriver.exe')
driver = webdriver.Firefox(executable_path=firefox_path)
logger.info('初始化Firefox浏览器并启动')
return driver
def __get_edge_driver(self):
"""
驱动下载地址:https://developer.microsoft.com/zh-cn/microsoft-edge/tools/webdriver/
:return:
"""
edge_path = os.path.join(current_path, '..', 'webdriver', 'edge', 'msedgedriver.exe')
driver = webdriver.Edge(executable_path=edge_path)
logger.info('初始化Edge浏览器并启动')
return driver
if __name__ == '__main__':
driver = BrowserUtils().get_driver()
driver.get("https://www.baidu.com")
print(driver.title)
driver.quit()
获取docker容器内容的环境变量
echo $PATH
系统管理==》系统设置==》全局属性
搭建的过程中主要是配置chrome的地址配置,以及环境变量的问题需要注意,还有就是你自己的代码里面需要注意使用无头模式,不然就会报错找不到对应的driver