吃灰电脑经过几天的折腾,首先放弃了Chrome方案,因为谷歌一早就不支持32位构架了,所以不是浏览器不行,就是驱动不行,最后放弃。
电脑配置:赛扬M440,1.5G内存,100G硬盘
系统配置:ubuntu 14.04(I386)32位
浏览器:Firefox 版本是 66.0.3(32位)
浏览器驱动:适配 Firefox 66.0.3 且支持 Linux 32 位的 GeckoDriver 0.24.0 版本(最后一个支持 32 位 Linux 的 GeckoDriver 版本)
步骤 1:下载适配的 GeckoDriver 0.24.0(32 位)
先删除旧的 GeckoDriver(若已下载),再下载新的适配版本:
bash
# 1. 移除旧版本(若有)
sudo rm -f /usr/local/bin/geckodriver ~/geckodriver
# 2. 下载GeckoDriver 0.24.0(32位Linux,适配Firefox 66)
cd ~ && wget https://github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-linux32.tar.gz -O geckodriver240.tar.gz
# 若github访问失败,用国内镜像:
# wget https://npmmirror.com/mirrors/geckodriver/v0.24.0/geckodriver-v0.24.0-linux32.tar.gz -O geckodriver240.tar.gz
# 3. 解压并配置全局可用
tar -zxvf geckodriver240.tar.gz
chmod +x geckodriver
sudo mv geckodriver /usr/local/bin/
# 4. 验证版本
geckodriver --version
✅ 成功标志:输出geckodriver 0.24.0 ( 2019-01-28)。
步骤 2:修改验证脚本(适配 Firefox 66.0.3)
Firefox 66 对参数的支持与 56.x 略有不同,调整脚本参数:
bash
nano ~/test_selenium.py
适配脚本:
bash
# Ubuntu 14.04 + Firefox 66.0.3 + GeckoDriver 0.24.0 验证脚本
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service
import sys
# 强制修复urllib3超时参数(解决类型错误)
import urllib3
urllib3.util.timeout.Timeout.DEFAULT_TIMEOUT = 30
# 替换urllib3超时验证逻辑(兜底)
original_validate = urllib3.util.timeout.Timeout._validate_timeout
def patched_validate(self, value, name):
try:
return original_validate(self, int(value) if value else 30, name)
except:
return original_validate(self, 30, name)
urllib3.util.timeout.Timeout._validate_timeout = patched_validate
def main():
try:
# 1. Firefox 66专属配置(适配32位低配置)
firefox_options = Options()
firefox_options.add_argument('--headless') # 无界面模式(必加)
firefox_options.add_argument('--no-sandbox') # Ubuntu 14.04必加
firefox_options.add_argument('--disable-gpu') # 禁用GPU(老旧CPU)
# 关闭无用功能,减少资源占用
firefox_options.set_preference('browser.startup.page', 0)
firefox_options.set_preference('browser.startup.homepage', 'about:blank')
firefox_options.set_preference('network.http.timeout', 30000) # 网络超时30秒
# 2. 手动指定GeckoDriver路径,避免自动查找冲突
gecko_driver_path = '/usr/local/bin/geckodriver'
service = Service(executable_path=gecko_driver_path)
# 3. 创建Firefox实例(适配66版本)
driver = webdriver.Firefox(
service=service,
options=firefox_options
)
driver.set_page_load_timeout(30)
print("✅ GeckoDriver 0.24.0与Firefox 66.0.3连接成功!")
# 4. 访问本地空白页(无网络/SSL冲突)
driver.get("about:blank")
print(f"✅ 空白页访问成功,标题:{driver.title}")
# 可选:访问HTTP百度(避开HTTPS,适配老旧SSL库)
# driver.get("http://www.baidu.com")
# print(f"✅ 百度访问成功,标题:{driver.title}")
# 5. 关闭浏览器
driver.quit()
service.stop()
print("🎉 自动化程序验证成功!")
except Exception as e:
print(f"❌ 验证失败,错误:{str(e)}")
# 版本检查(关键)
try:
import subprocess
print("\n🔍 版本信息:")
print("Firefox:", subprocess.check_output(['firefox', '--version']).decode().strip())
print("GeckoDriver:", subprocess.check_output(['geckodriver', '--version']).decode().strip())
print("Selenium:", __import__('selenium').__version__)
except:
pass
sys.exit(1)
if __name__ == "__main__":
main()
保存退出:Ctrl+O → 回车 → Ctrl+X。
步骤 3:运行验证脚本
bash
python3 ~/test_selenium.py
✅ 成功标志(终端输出)
bash
✅ GeckoDriver 0.24.0与Firefox 66.0.3连接成功!
✅ 空白页访问成功,标题:about:blank
🎉 自动化程序验证成功!
❌ 常见报错及解决
- SessionNotCreatedException :版本仍不匹配 → 确认
geckodriver --version是 0.24.0,firefox --version是 66.0.3; - Permission denied :GeckoDriver 无执行权限 → 重新执行
chmod +x /usr/local/bin/geckodriver; - Timeout :网络 DNS 问题 → 仅访问
about:blank即可,无需访问百度。
总结
- 核心适配:Firefox 66.0.3 必须搭配 GeckoDriver 0.24.0(支持 32 位 Linux 的版本);
- 关键修改:脚本中用
Service手动启动 GeckoDriver,修复 urllib3 超时参数逻辑; - 避坑点:优先访问本地空白页,避开 HTTPS/SSL 的老旧库冲突,确保低配置 CPU 能运行。