centos+selenium

安装selenium 

pip install selenium

安装谷歌浏览器

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

ps:可能会出现这种情况:

原因分析:下载python3.8的时候不小心把python2的软链接删掉了,yum默认使用的python版本是python2,导致yum不能使用

幸亏没有删掉python2.7

先查看python的所有链接

ls -la python*

发现python2.7还可以,直接让python2指向python2.7

ln -s /usr/bin/python2.7 /usr/bin/python2

 

 查看安装的谷歌版本

yum list | grep chrome

下载对应版本的驱动:在这个网站找到差不多的版本:(这里会出错,直接跳下一步)http://chromedriver.storage.googleapis.com/index.html

wget http://chromedriver.storage.googleapis.com/114.0.5735.90/chromedriver_linux64.zip

安装zip和unzip的驱动

yum list zip unzip
yum install -y zip unzip

将zip压缩包解压到指定文件下

参数-d:指定解压文件夹后存储的位置

unzip -d /opt/google/ chromedriver_linux64.zip.1

新建pytest.py文件(i是插入)

vim pytest.py

from selenium.webdriver import Chrome, ChromeOptions


url = 'http://www.baidu.com'
driver_path = '/opt/google/chromedriver-linux64/chromedriver'
chrome_option = ChromeOptions()
chrome_option.headless = True
chrome_option.add_argument('--no-sandbox') #解决DevToolsActivePort文件不存在的报错
chrome_option.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
chrome_option.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面

browser = Chrome(executable_path=driver_path, options=chrome_option)
browser.get(url)
browser.save_screenshot('/opt/google/screenshot1.png')
browser.quit() #不要忘记关闭浏览器

 保存退出(退出命令是,按ESC键 跳到命令模式,然后输入:q(不保存)或者:wq(保存) 退出)

(不要忘记加冒号..........)

:wq

运行pytest文件

python3 pytest.py

 报错了

分析原因是安装的浏览器和驱动版本不一致导致的,因为一开始的网站最新版本到114,以为114就可以,结果不行,找到了另外一个镜像网站有最新版本:Chrome for Testing availability (googlechromelabs.github.io)

把之前的卸载掉重新安装

wget https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/120.0.6099.71/linux64/chromedriver-linux64.zip

重新执行代码,又报错啦!!!(没有关系,再来!!!)

 分析原因:没有权限导致

分配权限:chmod 777 chmod 777 /opt/google/chromedriver-linux64/chromedriver

仔细一看并不是这个原因,是因为驱动的路径搞错了,我太马虎了,完整的路径是 /opt/google/chromedriver-linux64/chromedriver  没有在后面加chromedriver

重新执行一遍执行成功!!!

参考文章:在Linux上部署selenium,安装google-chrome和chromedriver-CSDN博客linux无界面模式安装selenium+chrome+chromedriver并成功完成脚本(亲测可用) - zfno11 - 博客园 (cnblogs.com)Linux无界面模式下使用selenium_linux不装浏览器可以进行seleium-CSDN博客
Selenium安装WebDriver最新Chrome驱动(含116/117/118/119)_chromedriver-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值