文章目录

  • 一、前言
  • 二、安装
    • 1.lunix下的pip换源
    • 2.安装selenium
    • 3.更新ubuntu
    • 4.安装谷歌浏览器
    • 5.安装chromedriver
  • 三、测试

一、前言

使用 selenium 的前提是 下载并安装一款 selenium 支持的浏览器浏览器驱动,并且二者版本要一致,我这里安装的是 chrome 和 chromedriver


二、安装

1.lunix下的pip换源

python -m pip install -U pip
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

2.安装selenium

pip install selenium

3.更新ubuntu

apt --fix-broken -y install
apt -y update
apt -y upgrade

4.安装谷歌浏览器

  1. 访问官网 https://www.google.cn/chrome/ 下载chrome
  2. 将页面拖至底部→其他平台→Linux
  3. 选择 .deb 文件→接收并安装
  4. 服务器创建 /ps/chrome 目录,将 .deb 文件上传至目录下
# 创建目录
mkdir -p /ps/chrome
# 手动上传 .deb 文件
  1. 安装 chrome
# 进入目录
cd /ps/chrome
# 安装依赖
apt -y install libxss1 libappindicator1 libindicator7
apt --fix-broken -y install
# 安装浏览器
dpkg -i google-chrome*.deb
# 查看版本
google-chrome --version
# Google Chrome 101.0.4951.64

5.安装chromedriver

  1. 根据上一步返回的 chrome 版本下载同版本的 chromedriver
    访问国内镜像 https://registry.npmmirror.com/binary.html?path=chromedriver/ 下载

  2. 同样上传到 /ps/chrome 目录下

  3. 安装chromedriver

# 安装unzip
apt install unzip
# 解压
unzip chromedriver_linux64.zip
# 移动并创建软连接到默认路径,后续启动selenium时就不需要指定chromedriver的路径了
mv chromedriver /usr/local/share/chromedriver
ln -s /usr/local/share/chromedriver /usr/bin/chromedriver

三、测试

创建测试文件 test_selenium.py

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Optionsif __name__ == '__main__':# 加载谷歌浏览器驱动chrome_options = Options()# linux下运行记得加上这些参数 ----------------------------chrome_options.add_argument('--headless')chrome_options.add_argument('--no-sandbox')chrome_options.add_argument('--disable-gpu')chrome_options.add_argument('--disable-dev-shm-usage')# -----------------------------------------------------# 加载chromedriver -------------------------------------------------# windows 下的 chromedriver 默认加载路径是当前路径下的 chromedriver.exe# linux 下的 chromedriver 默认加载路径是 /usr/bin/chromedriver# 当然也可以通过 executable_path 自定义driver = webdriver.Chrome(options=chrome_options)# -----------------------------------------------------------------# 打开百度首页driver.get('https://www.baidu.com/')# 获取百度导航栏中的文本xp = '//*[@id="s-top-left"]/a'nav_list = [elm.get_attribute('text') for elm in driver.find_elements(by=By.XPATH, value=xp)]print(nav_list)# ['新闻', 'hao123', '地图', '贴吧', '视频', '图片', '网盘']

run一下

python test_selenium.py

ubuntu安装selenium相关推荐

  1. Ubuntu 18.0.4 安装Selenium 详细流程(亲测有效)

    系统版本:Ubuntu 18.0.4 Python版本:python3.6.6 Selenium版本:3.14.0 firefox版本:52.0.2 (各版本下载地址) geckodriver版本:0 ...

  2. Ubuntu下selenium+Chrome的安装使用

    Ubuntu下selenium+Chrome的安装使用 安装 chrome 官网下载安装包 sudo dpkg -i google-chrome-stable_current_amd64.deb wh ...

  3. [How TO]-ubuntu下安装selenium

    环境:window10宿主机+ virtualbox + ubuntu20.10虚拟机 1.安装火狐浏览器 下载firefox版本:firefox-52.0.2.linux-x86_64.sdk.ta ...

  4. 一篇解决:Ubuntu安装配置、软件、工具、快捷键

    Ubuntu安装配置.软件.工具.快捷键 记录作者配置windows.Ubuntu双系统的各种细节. 此帖说明了Ubuntu初期配置的许多问题,也介绍了很多使用技巧,持续更新. 文章目录 Ubuntu ...

  5. Ubuntu安装MongoDB

    https://jingyan.baidu.com/article/e5c39bf5f5ddd539d76033a9.html Ubuntu安装MongoDB4.0: https://www.jian ...

  6. ubuntu安装OpenCV详细教程(建议收藏)

    记录一篇"如何安装ubuntu下C++接口的opencv"的文章.由于转载格式有问题,直接贴个链接:ubuntu安装opencv的正确方法 注意:本人根据此博客安装遇到的问题:2. ...

  7. Python 安装selenium

    一.报错信息 No module named 'selenium' 二.系统环境 操作系统:Win10 64位 Python版本:Python 3.7.0 三.安装参考 1.使用pip安装seleni ...

  8. Linux/ubuntu 安装 redis 4.0报错解决:redis-server.service: Can't open PID file /var/run/redis/redis-server.

    此文首发于我的个人博客:Linux/ubuntu 安装 redis 4.0报错解决:redis-server.service: Can't open PID file /var/run/redis/r ...

  9. debian,ubuntu 安装mongodb 允许外网访问,修改端口,设置用户和密码

    使用apt安装mongodb: apt update && apt install mongodb 查看运行状态: systemctl status mongodb.service 结 ...

最新文章

  1. 大脑也在强化学习!加州大学提出「价值决策」被大脑高效编码,登Neuron顶刊...
  2. IB网络用户数量超过私有网络近4倍
  3. Spring 捕捉校验参数异常并统一处理
  4. 深入理解Java注解Annotation及自定义注解
  5. oracle in与exists的使用
  6. Matlab非线性方程求解
  7. html服务器端运行python,如何通过点击网页在远程服务器上运行python脚本?
  8. js和css实现手机横竖屏预览思路整理
  9. ODI配置Mysql5.1数据库服务器
  10. oracle数据库物理结构包含,Oracle - 数据库物理结构
  11. python直方图与x轴对应不起来_Python Matplotlib绘图与x轴标签正确对齐并匹配系列和错误的颜色...
  12. 小米4391人获1.749亿股股票的激励
  13. [CTO札记]给新助理的3句话
  14. ASM磁盘超过disk_repair_time导致磁盘状态为forcing
  15. 链接mysql工具封装_JDBC——java连接MySQL封装处理
  16. Matlab GUI编程技巧(四):m文件与simulink之间数据传递
  17. Sm4【国密4加密解密】实战
  18. java if 分号_if语句后面一定不能写分号吗?
  19. luoguP4578_ [FJOI2018]所罗门王的宝藏
  20. 一次性餐巾行业调研报告 - 市场现状分析与发展前景预测

热门文章

  1. oracle 11g r2 rac +openfiler 2.99 +centos 6.5+vbox
  2. Python2TheMax-欧阳桫-专题视频课程
  3. 计算机cpu任务管理器,任务管理器cpu多少才是正常
  4. 微信小程序开发步骤+底部菜单栏制作
  5. Minecraft TrMenu 菜单插件的JS动作高级用法
  6. Skype for Windows 10脱离Preview标签 已满足日常使用
  7. byte数组与字符串之间相互转换
  8. 英雄联盟在选择大区后闪退,导致不能进行游戏
  9. java keyset entryset 顺序_「entryset」如何遍历Map,map的keySet()和EntrySet()区别 - seo实验室...
  10. ORA-02063: preceding line from DBLINK