CentOS 命令行 Selenium 自动测试

it2025-10-20  8

1、安装Chrome

配置Google的epel源

vi /etc/yum.repos.d/google.repo [google] name=Google-x86_64 baseurl=http://dl.google.com/linux/rpm/stable/x86_64 enabled=1 gpgcheck=0 gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
# 更新yum yum update # 安装Chrome yum install google-chrome-stable

2、ChromeDriver下载

查看Chrome版本

google-chrome --version

下载:https://npm.taobao.org/mirrors/chromedriver/

找到Chrome对应的ChromeDriver版本,并下载

wget https://npm.taobao.org/mirrors/chromedriver/86.0.4240.22/chromedriver_linux64.zip

将下载解压的ChromeDriver放到脚本同级目录调用

3、ChromeDriver授权

chmod 755 /home/pysrcript/chromedriver

4、测试py脚本

from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument("--headless") # 无头模式 chrome_options.add_argument('--disable-gpu') # 禁用GPU加速 chrome_options.add_argument('--no-sandbox') # 禁止沙箱模式 url="https://blog.csdn.net/" brower=webdriver.Chrome(executable_path="./chromedriver", options=chrome_options) brower.get(url) source = brower.page_source #获取网页源代码 print(source) brower.quit()

关于博主

如果你喜欢我的博文就给我点个赞 或 赞个赏吧~ 3.65元,一年365天继续分享创作!

最新回复(0)