ubuntu16.04下配置selenium+chrome

参考地址:https://blog.csdn.net/fengltxx/article/details/79622854

 

1、谷歌浏览器及驱动:

谷歌浏览器下载地址:https://www.chromedownloads.net/

谷歌浏览器驱动下载地址:http://chromedriver.storage.googleapis.com/index.html

下载驱动时要对应好版本,在谷歌浏览器驱动地址中的notes.txt可以查看支持的浏览

 2、ubuntu上安装谷歌浏览器和驱动

(1)我是用的xshell+xftp,将本地下载好的谷歌浏览器放到ubuntu的某个文件夹下,比如:我放到的是:/home/shuai/chrome的文件夹下面。

参考地址:https://www.cnblogs.com/hupeng1234/p/6956500.html

cd到保存浏览器的文件夹中,使用下面命令安装

sudo apt-get install libappindicator1 libindicator7
sudo dpkg -i google-chrome-stable_current_amd64.deb   # -i后面是你自己的文件名字
sudo apt-get -f install

(2)安装谷歌浏览器驱动

参考地址:https://blog.csdn.net/fengltxx/article/details/79622854

在本地解压下载的驱动包,进去有一个Chromedriver,将这个文件上传到ubuntu上的某个文件夹中,比如:我放在/home/shuai/chrome_driver/文件夹下。

进入目录下,使用 sudo mv chromedriver /usr/bin/  将驱动放到/usr/bin/目录下面。

注意:在最后运行测试文件的时候,会出现“permission XXX”的问题,这就需要进入ubuntu界面手动修改文件可以被程序执行。如下面界面:

 Allow executing file as program 前面的勾打上。

3、安装selenium

pip insntall selenium    # python2.x版本

pip3 install selenium   # python3.x版本

4、测试脚本:

test.py

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os

chrome_options = webdriver.ChromeOptions()
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 = "/usr/bin/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chrome_options=chrome_options,executable_path=chromedriver)
driver.get("https://www.baidu.com")
print(driver.title)
driver.quit()

python3 test.py结果为:百度一下,你就知道!

如果启动之后,没有报错,但是也没有任何结果,考虑可能是谷歌浏览器没法上网,在界面化命令中启动google-chrme,输入百度地址,如果报下面错误:

NSS_VersionCheck("3.26") failed. NSS >= 3.26 is required. Please upgrade to the latest NSS, and if you still get this error, contact your distribution maintainer. 

参考地址:https://majing.io/posts/10000000861151

使用下面命令更新libnss3

sudo apt-get install libnss3
sudo apt-get update

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅帅的Python

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值