python chromedriver with selenium on linux

本文介绍了如何在CentOS7环境下配置Python的Selenium库与Chrome浏览器的Chromedriver进行自动化测试。详细过程包括下载与安装Chrome浏览器、设置字体、通过pip安装Selenium、解决Chromedriver的下载与执行问题,以及如何在无窗口模式下运行Chrome进行自动化测试。

为要考虑写一个python应用, 查资料时无意发现老朋友selenium已经发展到和chrome很深入的阶段。
chrome为了配合自动测试的需求,甚至专门开发chromedriver这样的东西,一时技痒尝试了一下
环境是前面搭好的centos7
google.com/chrome是墙外无法访问了,幸好还有它自家分号
https://www.google.cn/chrome/
windows下访问自动判断给的是windows安装包,要拉到最下选择其他平台的,然后选择“64 位 .rpm(适用于 Fedora/openSUSE)”,得到的是一个
google-chrome-stable_current_x86_64.rpm
然后yum安装chrome以及中文字体

# yum install google-chrome-stable_current_x86_64.rpm 
# yum install google-noto-sans-simplified-chinese-fonts

selenium略为麻烦,它要从python安装,要用到pip
pip又不在centos的标准repo里,我选择先安装epel源,

# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# yum install python2-pip python2-wheel
# pip install selenium

chromedriver自身地址也在墙外,幸好国内有个mirror
http://npm.taobao.org/mirrors/chromedriver

选择了上面最新的76.0.3865.10,虽然我下载的chrome版本以及到了76.0.3865.100
但大版本一致就没问题,下载后是个zip文件 chromedriver_linux64.zip
unzip 解压之得到一个单独的可执行文件 chromedriver

终于可以开始小福利一下,谁让csdn上python主和selenium下面最热的居然不是自动测试,而是各种福利呢

先解决chrome不能直接在centos服务器环境里启动的事

# export DISPLAY=192.168.0.102:0.0    
## 服务器上没有安装Xserver,不过MobaXterm自带的有,很好很方便
# ./chromedriver &
# vim demo.py

然后在demo.py里输入代码, 为免广告嫌疑我把网址马赛克了

from selenium import webdriver

cap = webdriver.DesiredCapabilities.CHROME.copy()
browser = webdriver.Remote(command_executor="http://localhost:9515", desired_capabilities=cap);
browser.get('https://手工马赛克/tags-all.html')

福利截图被拦截了,没得妹子看
成功后地址栏下面有一行"Chrome is being controlled by automated test software"

对测试和爬虫需求来说,chrome还有个更强大的特性是headless模式,全程无窗口模拟真实浏览器
这次不打算往下撸了,有空再搞

<think> Hmm, the user is reporting an issue with ChromeDriver located at a specific path being invalid. Based on the error message fragment, this is clearly related to Selenium automation. First, I need to address the core problem - an invalid ChromeDriver executable. The most common causes include version mismatch with Chrome browser, file corruption, or insufficient permissions. Since the user didn't specify their OS, I should cover solutions for all major platforms. The user seems technical but might be frustrated since automation tools failing can block their workflow. I should provide clear step-by-step solutions starting with the most likely fix (version compatibility). The permission issue is worth mentioning since it's often overlooked, especially on Linux/Mac. For the required follow-up questions, I'm considering these angles: One about silent installation methods since power users might need that. Another about headless mode because many automation scenarios run without GUI. Environment variables question since PATH configuration is fundamental. And verification steps because confirming the fix is crucial. These cover troubleshooting depth while potentially uncovering if the user is doing more advanced automation. The "invalid" error is vague, so these questions help diagnose the root cause better. </think> 关于Chromedriver路径无效的错误,通常由以下原因及解决方案: **常见原因及解决步骤:** 1. **版本不匹配**(最常见) - 检查Chrome浏览器版本(地址栏输入 `chrome://version`) - 到[Chromedriver官网](https://chromedriver.chromium.org/)下载对应版本 - 替换旧驱动文件 2. **文件损坏/权限问题** ```bash # Linux/Mac 终端执行(替换路径) chmod +x /your/path/chromedriver ``` - Windows:右键文件 → 属性 → 取消"阻止不安全文件" 3. **路径配置错误** - 确认路径中的特殊字符(空格/中文需转义) - 代码中应使用原始字符串或双反斜杠: ```python # Python示例 driver = webdriver.Chrome(executable_path=r"C:\path\with\space\chromedriver.exe") ``` 4. **环境变量配置** - 将chromedriver所在目录加入系统PATH - 重启命令行/IDE生效
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值