一.环境变量配置(普遍,可网上自己百度)
1.驱动下载地址:Google Chrome 64bit Windows版_chrome浏览器,chrome插件,谷歌浏览器下载,谈笑有鸿儒
2.查看版本
chromedriver --version
二.Webdriver Manager
1.github地址:https://github.com/SergeyPirogov/webdriver_manager
安装驱动:pip install webdriver-manager
本地启动示例:
# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
# 浏览器启动选项
options = webdriver.ChromeOptions()
# 启用无界面界面模式
options.add_argument('--headless')
# 谷歌浏览器跳过【你的连接不是私密连接】
options.add_argument('ignore-certificate-errors')
# 窗口设置为1960,1080
options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)
driver.get("https://www.baidu.com")
title = driver.title
print(title)
driver.quit()
远程启动示例:
from selenium import webdriver
# 浏览器启动选项
options = webdriver.ChromeOptions()
# 启用无界面界面模式
options.add_argument('--headless')
# 谷歌浏览器跳过【你的连接不是私密连接】
options.add_argument('ignore-certificate-errors')
# 窗口设置为1960,1080
options.add_argument("--window-size=1920,1080")
driver = webdriver.Remote(command_executor='http://localhost:4444', options=options)
driver.get("https://www.baidu.com")
title = driver.title
print(title)
driver.quit()