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 update , 然后yum install google-chrome-stable
查看安装的版本
google-chrome --version
2. chromedriver下载
网址:https://npm.taobao.org/mirrors/chromedriver
找到chrome对应的chromedriver 版本,并下载
wget https://registry.npmmirror.com/-/binary/chromedriver/101.0.4951.15/chromedriver_linux64.zip
将下载的chromedriver 放到脚本同级目录调用
3 . 为chromedriver授权
chmod 755 chromedriver
4. 测试代码 ts.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36')
options.add_argument("--headless")
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox') # 禁止沙箱模式,否则肯能会报错遇到chrome异常
url="https://www.west.cn/login.asp"
brower=webdriver.Chrome(executable_path="./chromedriver", options=options)
brower.get(url)
print(brower.current_url)
brower.get("https://www.west.cn/Manager/")
print(brower.current_url)
brower.quit()