0%

安装chrome_ 使用selenium

安装chrome_ 使用selenium

1、查看 chrome 版本

google-chrome --version

# centos7 selenium 版本更新
# 执行 wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm下载最新Linux的chrome
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

# 下载之后执行sudo yum localinstall google-chrome-stable_current_x86_64.rpm安装本地的rpm包则会替换之前版本的chrome
sudo yum localinstall google-chrome-stable_current_x86_64.rpm

2、更新 || 禁止自动更新chrome

# 禁止自动更新
echo 'exclude=google-chrome-stable' >> /etc/yum.conf

# 更新 chrome
sudo yum update google-chrome-stable

3、安装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 update # 更新
yum install google-chrome-stable # 安装

4、chromedriver下载

淘宝源 下载
官方源 下载

chromedriver新版本(116+)

找到chrome对应的chromedriver 版本,并下载

wget https://chromedriver.storage.googleapis.com/74.0.3729.6/chromedriver_linux64.zip

将下载的chromedriver 放到脚本同级目录调用


5 、为chromedriver授权

chmod 755 chromedriver

6、测试代码 ts.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')
chrome_options.add_argument('--no-sandbox') # 禁止沙箱模式,否则肯能会报错遇到chrome异常
url="https://www.west.cn/login.asp"
brower=webdriver.Chrome(executable_path="./chromedriver", chrome_options=chrome_options)
brower.get(url)
print(brower.current_url)
brower.get("https://www.west.cn/Manager/")
print(brower.current_url)
brower.quit()

7、测试结果

img