当前位置:首页 > python > 正文内容

在Linux操作系统上使用selenium库

zhangsir3年前 (2022-08-23)python319

安装selenium模块

命令:

pip3 install selenium


安装chrome命令:

yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm


这个命令是下载安装最新的稳定的chrome版本,不是固定的版本,所以要注意下载chromedriver时要对应版本


查看版本

google-chrome --version


我是在本地win10系统又下了一遍,解压可以看到chrome版本是86


注:如果运行程序出错:Cannot find Chrome binary,就是没有安装chrome


安装依赖库

命令:

yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 -y


注:如果没有安装依赖库会报错:error while loading shared libraries: libX11.so.6:


安装chromedriver(驱动程序)


下载链接如下:点我下载,一定要找到与上面chrome版本相应的版本,这里下载和上面对应的86版本


可以windows下载后然后传到服务器上,也可以直接使用wget命令下载


命令:

wget http://chromedriver.storage.googleapis.com/index.html?path=86.0.4240.22/


然后给chromedriver 文件赋予可执行权限


命令:

chmod 777 chromedriver


然后放到环境变量PATH路径中


命令:

cp chromedriver /usr/bin/


可以查看chromedriver的版本号


命令:

chromedriver --version


selenium代码测试


在服务器上新建一个python文件,写入以下代码


#!/usr/bin/python3
#coding:utf-8
from selenium import webdriver
ch_options = webdriver.ChromeOptions()
#为Chrome配置无头模式
ch_options.add_argument("--headless")  
ch_options.add_argument('--no-sandbox')
ch_options.add_argument('--disable-gpu')
ch_options.add_argument('--disable-dev-shm-usage')
# 在启动浏览器时加入配置
dr = webdriver.Chrome(options=ch_options)
#这是测试网站
url = "https://www.baidu.com"
dr.get(url)
#打印源码
print(dr.page_source)


然后python执行这个py文件,结果打印出了源码,即selenium模块环境配置成功。


zhangsir版权a1防采集https://mianka.xyz

扫描二维码推送至手机访问。

版权声明:本文由zhangsir or zhangmaam发布,如需转载请注明出处。

本文链接:https://www.mianka.xyz/post/15.html

分享给朋友:

“在Linux操作系统上使用selenium库” 的相关文章

python之seleniumwire获取network(网络)信息

python之seleniumwire获取请求头参数import time from seleniumwire import webdriver # 创建Chrome驱动程序的新实例 driver = webdriver...

pip安装三方库 国内的一些镜像站点推荐

pip 国内的一些镜像站点推荐镜像套路:使用cmd;输入命令pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple 包名 即可开始安装。清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http...

python selenium find_element_by_xpath 方法已经被弃用的解决办法

背景:在使用最新3.10.4Python版本时候,用selenium进行xpath定位元素,编译器提示:DeprecationWarning:find_element_by_xpath is deprecated. Please use find_element(by=By.XPATH, value...

python 爬虫 报错:UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0x8b in position”解决方案

发现报错“UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1:invalid start byte”,方法一:根据报错提示,错误原因有一条是这样的:“'Accept-Encodi...

python—pymysql的增删改查操作实例展示

Python使用pymysql连接数据库1.导包import pymysql2.连接数据库connection = pymysql.connect(     host='',  # ...

Linux系统下使用Python+selenium+谷歌浏览器下载文件

from seleniumwire import webdriver import time ch_options = webdriver.ChromeOptions() ch_options.add_argument("-...