Ubuntu【带界面】配置chromedriver环境:
1. 卸载已存在的Chrome(报错或者没有装过,可以跳过)
sudo apt-get remove google-chrome-stable
2. 访问呢Chrome官网下载Linux版本Chrome
https://www.google.cn/intl/zh-CN/chrome/
3. 安装Chrome
sudo apt-get install [安装包名]
4. 查看Chrome版本
google-chrome --version
5. 查看应该下载的Chromedriver版本
curl "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_【版本】"
例:curl "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_110.0.5481"
6. 下载Chromedriver
http://chromedriver.storage.googleapis.com/index.html
测试代码
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
def get_driver(retry=3):
flag=False
while not flag and retry>0:
try:
options = Options()
options.add_argument('--no-sandbox') # # Bypass OS security model
#options.add_argument("--headless")
options.add_argument('disable-infobars')
options.add_argument("--disable-extensions")
options.add_argument("--remote-debugging-port=9222")
# 自动下载驱动
# driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
# 加载本地驱动
driver = webdriver.Chrome("/home/baidu/GetCookie/chromedriver",options=options)
flag=True
return driver
except Exception as e:
retry-=1
print(f"{e}{e.__traceback__.tb_lineno}")
return "get driver flase"
driver = util_tools.get_driver()
target_url = 'https://www.ixigua.com/?wid_try=1'
# driver.set_window_size(1120, 550)
driver.get(target_url)
# time.sleep(3)
cookie_list = driver.get_cookies()
cookie_map = {}
for args in cookie_list:
cookie_map[args["name"]] = args["value"]
print(cookie_map)
driver.quit()