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

在Linux操作系统上使用selenium库

zhangsir3年前 (2022-08-23)python385

安装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版权h8防采集https://mianka.xyz

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

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

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

分享给朋友:

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

如何用python获取一个网页的所有连接

如何用python获取一个网页的所有连接很简单直接上代码:# -*- coding: utf-8 -*- ''' 如何用python获取一个网页的所有连接 author:zhangsir ''' imp...

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

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

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 给电脑设置闹钟

python会自动触发windows桌面通知,提示重要事项,比如说:您已工作两小时,该休息了我们可以设定固定时间提示,比如隔10分钟、1小时等用到的第三方库:win10toast - 用于发送桌面通知的工具from win10toast import ToastNoti...

python 将json数据转成csv文件

从JSON数据转化CSV文件下面的这个Python脚本能够将JSON数据转化到CSV文件的表格当中去,我们输入的是带有.json后缀的文件,输出的是.csv后缀的表格文件,代码如下import json def converter(input_file, output...

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

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