Linux环境下Selenium与Chrome自动化配置及常见问题解决
本文详细介绍在Linux系统中安装Chrome浏览器及Chromedriver的方法,推荐使用淘宝镜像源加速下载。重点解析Python Selenium脚本配置,包括无头模式、沙箱参数及字体安装以解决中文乱码。同时针对截图时常见的TimeoutException异常,提供通过添加headless及禁用GPU等参数的具体解决方案。
Chrome与Chromedriver环境安装
在Linux环境中使用Selenium进行自动化操作,首先需要确保系统中已安装Google Chrome浏览器。
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
yum install ./google-chrome-stable_current_x86_64.rpm
yum install mesa-libOSMesa-devel gnu-free-sans-fonts wqy-zenhei-fonts
接下来需要安装与Chrome版本匹配的Chromedriver驱动,以便Selenium能够控制浏览器。
为了加快下载速度并提高稳定性,建议使用国内镜像源进行下载。
wget http://npm.taobao.org/mirrors/chromedriver/2.41/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
move chromedriver /usr/bin/
chmod +x /usr/bin/chromedriver
Selenium自动化脚本配置
完成环境配置后,即可开始编写基于Selenium的Python自动化脚本。
#!/usr/bin/python
# -*-coding:utf-8-*-
import re, os
import json
import time
import random
import requests
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.wait import TimeoutException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.select import Select
binary_location = '/usr/bin/google-chrome'
chrome_driver_binary = '/usr/bin/chromedriver'
chrome_options = Options()
chrome_options.binary_location = binary_location
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')
chromedriver = chrome_driver_binary
os.environ["webdriver.chrome.driver"] = chromedriver
BROWSER = webdriver.Chrome(executable_path='/usr/bin/chromedriver', chrome_options=chrome_options)
WAIT = WebDriverWait(BROWSER, 5)
URL = "http://www.baidu.com"
BROWSER.get(URL)
..........
常见故障排查与解决方案
在实际使用过程中,可能会遇到一些常见的问题,例如中文显示异常。
针对中文乱码问题,可以通过安装特定的中文字体包来解决。
对于CentOS系统,可以通过yum命令安装所需的字体组。
yum groupinstall fonts
对于Ubuntu系统,则需使用apt-get命令安装特定的中文字体包。
sudo apt-get install ttf-wqy-microhei ttf-wqy-zenhei xfonts-wqy
另一个常见的问题是截图功能失效,并抛出超时异常。
具体表现为程序无法完成截图操作,并抛出time out异常。
错误信息通常显示为:selenium.common.exceptions.TimeoutException: Message: timeout: Timed out receiving message from renderer: 10.000
解决该问题的方法是在ChromeOptions中添加特定的启动参数。
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("start-maximized")
options.add_argument("enable-automation")
options.add_argument("--no-sandbox")
options.add_argument("--disable-infobars")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-browser-side-navigation")
options.add_argument("--disable-gpu")
driver = webdriver.Chrome(chrome_options=options)
driver.set_window_size(1024, 768)
driver.get_screenshot_as_file(STATIC_FOLDER + home_img_url)
driver.close()
至此,关于在Linux环境下使用Selenium和Chrome的完整配置及问题排查已介绍完毕。如需了解更多相关技术细节,建议查阅本站往期文章或关注后续更新内容,感谢支持。
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













